Servlet编程-步步为营
【环境】
eclipse j2ee;
Tomcat 7.0;
【模型1】
package com.zhiqi; import ...; public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
resp.setContentType ("text/html;charset=utf-8");
PrintWriter out=resp.getWriter();
out.println("<html>");
out.println("<head><title>test Servlet</title></head>");
out.println("<body>");
out.println("欢迎使用Servlet编程");
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet- class>com.zhiqi.TestServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/fun</url-pattern>
</servlet-mapping>
</web-app>
【模型2】getInitParameter("repeat")
package com.zhiqi; import ...; public class SecondServlet extends HttpServlet {
private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
int repeat=Integer.parseInt (getInitParameter("repeat"));
resp.setContentType ("text/html;charset=utf-8");
PrintWriter out=resp.getWriter();
out.println("<html>");
out.println("<head><title>test2 Servlet</title></head>");
out.println("<body>"); for(int i=0;i<repeat;i++){
out.println("<table border= \"1\"><tr>");
out.println("<td>"+(i +1)+"</td><td>");
out.println("欢迎使用Servlet");
out.println ("</td></tr></table>");
out.println("<br>");
}
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>SecondServlet</servlet- name>
<servlet- class>com.zhiqi.SecondServlet</servlet-class> <init-param>
<param-name>repeat</param-name>
<param-value>5</param-value>
</init-param>
</servlet> <servlet-mapping>
<servlet-name>SecondServlet</servlet- name>
<url-pattern>/second</url-pattern>
</servlet-mapping>
</web-app>
【模型3】获取服务器的运行参数
package com.zhiqi; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class ThirdServlet extends HttpServlet {
private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
doPost(req,resp);
} public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
resp.setContentType("text/html;charset=utf-8");
PrintWriter out=resp.getWriter();
out.println("<html>");
out.println("<head><title>test3 Servlet</title></head>");
out.println("<body>");
fun(req,out);
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
public void fun(HttpServletRequest req,PrintWriter out) throws IOException{
out.println("<ul>");
out.println("<li>服务器IP->"+req.getRemoteAddr()+"</li>");
out.println("<li>服务器名->"+req.getServerName()+"</li>");
out.println("<li>服务器端口->"+req.getServerPort()+"</li>");
out.println("<li>协议名->"+req.getScheme()+"</li>");
out.println("<li>协议版本->"+req.getProtocol()+"</li>");
out.println("<li>请求的方法->"+req.getMethod()+"</li>");
out.println("<li>请求的URL->"+req.getRequestURL()+"</li>");
out.println("<li>上下文路径->"+req.getContextPath()+"</li>");
out.println("<li>Servlet路径->"+req.getServletPath()+"</li>"); out.println("</ul>");
}
}
<?xml version="1.0" encoding="UTF-8"?> <web-app>
<servlet>
<servlet-name>ThirdServlet</servlet-name>
<servlet-class>com.zhiqi.ThirdServlet</servlet-class> </servlet> <servlet-mapping>
<servlet-name>ThirdServlet</servlet-name>
<url-pattern>/third</url-pattern>
</servlet-mapping>
</web-app>
【模型4】获得头信息
package com.zhiqi; import ... public class FourthServlet extends HttpServlet {
private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
fun(req,resp);
}
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
fun(req,resp);
}
public void fun(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
String headers=null;
Enumeration e=req.getHeaderNames();
resp.setContentType("text/html;charset=utf-8");
PrintWriter out=resp.getWriter();
out.println("<html>");
out.println("<head><title>test4 Servlet</title></head>");
out.println("<body>");
out.println("<ul>"); while(e.hasMoreElements()){
headers=(String) e.nextElement();
if(headers!=null){
out.println("<li>"+headers+" -> "+req.getHeader(headers)+"</li>");
//out.println("->");
//out.println("<li>"+req.getHeader(headers)+"</li>");
}
}
//out.println("<li>"+"</li>");
out.println("</ul>");
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
}
<?xml version="1.0" encoding="UTF-8"?> <web-app>
<servlet>
<servlet-name>FourthServlet</servlet-name>
<servlet-class>com.zhiqi.FourthServlet</servlet-class> </servlet> <servlet-mapping>
<servlet-name>FourthServlet</servlet-name>
<url-pattern>/fourth</url-pattern>
</servlet-mapping>
</web-app>
【模型5】Servlet处理表单
package com.zhiqi; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
resp.setContentType("text/html;charset=utf-8");
PrintWriter out=resp.getWriter();
out.println("<html>");
out.println("<head><title>login</title></head>");
out.println("<body>");
out.println("<h2>请输入登录信息</h2>");
out.println("<form method='post' action='"+req.getContextPath()//上下文路径
+"/f'"
+"'>");
out.println("<div>用户名:</div>");
out.println("<div><input type='text' name='userName' id='userName' /></div>"); out.println("<div>密码:</div>");
out.println("<div><input type='password' name='password' id='password' /></div>"); out.println("<div><input type='submit' value='提交' /></div></form>"); out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
resp.setContentType("text/html;charset=utf-8");
PrintWriter out=resp.getWriter();
req.setCharacterEncoding("utf-8");//解决提交的表单数据中文乱码
out.println("<html>");
out.println("<head><title>login info</title></head>");
out.println("<body>");
String userName=req.getParameter("userName");
String password=req.getParameter("password");
if(userName==null || userName.length()==0 || password==null || password.length()==0){
out.println("<h2>输入用户名和密码</h2>");
out.println("<br><a href='"
+req.getContextPath()+"/f'"
+">登录</a>");
}else{
out.println("<h2>用户信息</h2>");
out.println(userName);
out.println(password);
}
out.println("</body>");
out.println("</html>");
out.flush();
out.close(); }
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app> <servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.zhiqi.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/f</url-pattern>
</servlet-mapping> </web-app>
通过Servlet处理表单是非常麻烦的,1、需要调用out对象输出。2、双引号的转义
所以需要对此进行改造
模型【6】处理HTML页面的表单
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login page</title>
</head>
<body>
<h3>请输入用户名和密码</h3>
<div>
<form method="post" action="/LoginServlet2/f"><!-- 上下文路径+url-pattern -->
用户名:<input type="text" name="userName" ><br><br>
密码:<input type="password" name="password" ><br><br>
<input type="submit" value="登录">
</form>
</div> </body>
</html>
package com.zhiqi; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class LoginServlet2 extends HttpServlet {
private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
resp.setContentType("text/html;charset=utf-8");
PrintWriter out=resp.getWriter();
out.println("<html>");
out.println("<head><title>login</title></head>");
out.println("<body>");
out.println("<h2>请输入登录信息</h2>");
out.println("<form method='post' action='"+req.getContextPath()//上下文路径
+"/f'"
+"'>");
out.println("<div>用户名:</div>");
out.println("<div><input type='text' name='userName' id='userName' /></div>"); out.println("<div>密码:</div>");
out.println("<div><input type='password' name='password' id='password' /></div>"); out.println("<div><input type='submit' value='提交' /></div></form>"); out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
resp.setContentType("text/html;charset=utf-8");
PrintWriter out=resp.getWriter();
req.setCharacterEncoding("utf-8");//解决提交的表单数据中文乱码
out.println("<html>");
out.println("<head><title>login info</title></head>");
out.println("<body>");
String userName=req.getParameter("userName");
String password=req.getParameter("password");
if(userName==null || userName.length()==0 || password==null || password.length()==0){
out.println("<h2>输入用户名和密码</h2>");
out.println("<br><a href='"
+req.getContextPath()+"/f'"
+">登录</a>");
}else{
out.println("<h2>用户信息</h2>");
out.println(userName);
out.println(password);
}
out.println("</body>");
out.println("</html>");
out.flush();
out.close(); }
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app> <servlet>
<servlet-name>LoginServlet2</servlet-name>
<servlet-class>com.zhiqi.LoginServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet2</servlet-name>
<url-pattern>/f</url-pattern>
</servlet-mapping> </web-app>
【模型7】实例-网络调查表
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>网络调查表</title>
</head>
<body>
<h2>潜在用户调查</h2>
<div>
<form method="post" action="/Example1/f">
<div>
<div style="float:left;margin-left:50px;">姓名:</div>
<div style="float:left;margin-left:20px;"><input type="text" name="name"></div>
</div>
<div style="clear:both;">
<div style="float:left;margin-left:50px;margin-top:5px;">Email:</div>
<div style="float:left;margin-left:20px;margin-top:5px;"><input type="text" name="email"></div>
</div>
<div style="clear:both;">
<div style="float:left;margin-left:50px;margin-top:5px;">年龄:</div>
<div style="float:left;margin-left:20px;margin-top:5px;">
<input type="radio" name="age" value="小于18">小于18
<input type="radio" name="age" value="18-25">18-25
<input type="radio" name="age" value="26-40">26-40
<input type="radio" name="age" value="大于40">大于40
</div>
</div>
<div style="clear:both;">
<div style="float:left;margin-left:50px;margin-top:5px;">编程时间:</div>
<div style="float:left;margin-left:50px;margin-top:5px;">
<select name="seltime">
<option value="1-2年">1-2年</option>
<option value="2-5年">2-5年</option>
<option value="5-10年">5-10年</option>
</select>
</div>
</div>
<div style="clear:both;">
<div style="float:left;margin-left:50px;margin-top:5px;">使用操作系统:</div>
<div style="float:left;margin-left:20px;margin-top:5px;">
<select name="selos">
<option value="Win XP">Win XP</option>
<option value="Linux">Linux</option>
<option value="Mac OS">Mac OS</option>
</select>
</div>
</div>
<div style="clear:both;">
<div style="float:left;margin-left:50px;margin-top:5px;">使用编程语言:</div>
<div style="float:left;margin-left:20px;margin-top:5px;">
<input type="checkbox" name="lan" value="C">C
<input type="checkbox" name="lan" value="C++">C++
<input type="checkbox" name="lan" value="JAVA">JAVA
<input type="checkbox" name="lan" value="C#">C#
<input type="checkbox" name="lan" value="Python">Python
</div>
</div>
<div style="clear:both;">
<div style="float:left;margin-left:50px;margin-top:5px;">建议:</div>
<div style="float:left;margin-left:20px;margin-top:5px;">
<textarea name="ta"></textarea>
</div>
</div>
<div style="clear:both;">
<div style="float:left;margin-left:50px;margin-top:5px;"></div>
<div style="float:left;margin-left:20px;margin-top:5px;">
<input type="reset" name="" value="重置">
<input type="submit" name="" value="提交">
</div>
</div> </form>
</div>
</body>
</html>
package com.zhiqi; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class Example1 extends HttpServlet {
private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException{
doPost(req,resp);
}
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException{
resp.setContentType("text/html;charset=utf-8");
PrintWriter out=resp.getWriter();
req.setCharacterEncoding("utf-8");
out.println("<html>");
out.println("<head><title>网络调查</title></head>");
out.println("<body>"); out.println("<h3>用户出入了以下信息</h3>"); String name=req.getParameter("name");
out.println("<p>用户姓名:"+name+"</p>");
String email=req.getParameter("email");
out.println("<p>用户邮箱:"+email+"</p>");
String age=req.getParameter("age");
out.println("<p>用户年龄:"+age+"</p>");
String seltime=req.getParameter("seltime");
out.println("<p>编程时间:"+seltime+"</p>");
String selos=req.getParameter("selos");
out.println("<p>使用平台:"+selos+"</p>"); String [] lan=req.getParameterValues("lan");//获取数组
out.println("<p>使用编程语言</p><ul>");
for(int i=0;i<lan.length;i++){
out.println("<li>"+lan[i]+"</li>");
}
out.println("</ul>"); String ta=req.getParameter("ta");
out.println("<p>建议:"+ta+"</p>");
out.println("</body>");
out.println("</html>");
}
}
<?xml version="1.0" encoding="UTF-8"?> <web-app> <servlet>
<servlet-name>Example1</servlet-name>
<servlet-class>com.zhiqi.Example1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Example1</servlet-name>
<url-pattern>/f</url-pattern>
</servlet-mapping> </web-app>
运行
【模型8】枚举类型获取参数以及参数值
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>满意度调查</title>
</head>
<body>
<h3>枚举类型获取参数以及参数值</h3>
<h3>你对本网站提供的内容满意吗?</h3> <form method="post" action="/Example2/f">
<input type="hidden" name="reach_reco" value="满意度调查">
<input type="submit" name="subm_cho" value="满意">
<input type="submit" name="subm_cho" value="不满意">
</form> </body>
</html>
package com.zhiqi; import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class Example2 extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException{
doPost(req,resp);
}
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException{
resp.setContentType("text/html;charset=utf-8");
PrintWriter out=resp.getWriter();
req.setCharacterEncoding("utf-8");
out.println("<html>");
out.println("<head><title>网络调查</title></head>");
out.println("<body>");
out.println("<h3>信息如下</h3>"); //获取枚举类型,可在不知道参数名的情况下输出
Enumeration e=req.getParameterNames();
while(e.hasMoreElements()){
String str=(String) e.nextElement();
out.println("<p>"+str+"</p>"); String [] s=req.getParameterValues(str);
for(int i=0;i<s.length;i++){
out.println("<p>"+s[i]+"</p>");
}
} out.println("</body>");
out.println("</html>");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app> <servlet>
<servlet-name>Example2</servlet-name>
<servlet-class>com.zhiqi.Example2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Example2</servlet-name>
<url-pattern>/f</url-pattern>
</servlet-mapping> </web-app>
【模型9】Cookie的使用
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test Cookie</title>
</head>
<body> <h3>Test Cookie</h3>
<form method="post" action="/Example8/set">
用户名:<input type="text" name="userName" id="userName" />
<br><br>
<input type="submit" value="登录" />
<input type="reset" value="重置" />
</form>
</body>
</html>
package com.zhiqi; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class Test_setCookie extends HttpServlet {
private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException{
doPost(req,resp);
}
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException{
resp.setContentType("text/html;charset=utf-8");
PrintWriter out=resp.getWriter();
req.setCharacterEncoding("utf-8");
out.println("<html>");
out.println("<head><title>Test Cookie</title></head>");
out.println("<body>"); out.println("<h3>测试set</h3>"); String userName=req.getParameter("userName");
Cookie coo1=new Cookie("userName",userName);
coo1.setMaxAge(24*60*60*30);
resp.addCookie(coo1); out.println("<a href='"
+req.getContextPath()
+"/get'>"
+"查看Cookie</a>"); out.println("<p>上下文路径->"+req.getContextPath()+"</p>"); out.println("</body>");
out.println("</html>");
} }
package com.zhiqi; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class Test_getCookie extends HttpServlet {
private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException{
doPost(req,resp);
}
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException{
resp.setContentType("text/html;charset=utf-8");
PrintWriter out=resp.getWriter();
req.setCharacterEncoding("utf-8");
out.println("<html>");
out.println("<head><title>Test Cookie</title></head>");
out.println("<body>"); out.println("<h3>测试get</h3>"); Cookie[] cos=req.getCookies();
Cookie c=null;
for(int i=0;i<cos.length;i++){
c=cos[i];
if(c.getName().equals("userName")){
out.println("用户名"+c.getValue());
}
} out.println("</body>");
out.println("</html>");
} }
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>Test_setCookie</servlet-name>
<servlet-class>com.zhiqi.Test_setCookie</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>Test_setCookie</servlet-name>
<url-pattern>/set</url-pattern>
</servlet-mapping> <servlet>
<servlet-name>Test_getCookie</servlet-name>
<servlet-class>com.zhiqi.Test_getCookie</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>Test_getCookie</servlet-name>
<url-pattern>/get</url-pattern>
</servlet-mapping> </web-app>
【模型10】Session会话
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body> <form method="post" action="/Example9/f">
<input type="submit" value="点击" />
</form> </body>
</html>
package com.zhiqi; import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; public class TestSession extends HttpServlet {
private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException{
doPost(req,resp);
}
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException{
resp.setContentType("text/html;charset=utf-8");
PrintWriter out=resp.getWriter();
out.println("<html>");
out.println("<head><title>Test Session</title></head>");
out.println("<body>"); HttpSession session=req.getSession();//创建Session
Integer count=(Integer)session.getAttribute("count");
if(count==null){
count=new Integer(1);
}else{
count=new Integer(count.intValue()+1);
}
session.setAttribute("count", count);
session.setMaxInactiveInterval(3600);
//访问次数
out.println("您已经访问了 "+count+" 次<br>");
if(session.isNew()){
out.println("这是一个新的Session<br>");
}else{
out.println("这不是一个新的Session<br>");
}
//返回一个包含分配给该 session 会话的唯一标识符的字符串
out.println("getID<br>"+session.getId()+"<br>");
//返回客户端最后一次发送与该 session 会话相关的请求的时间
out.println("getLastAccessedTime<br>"+session.getLastAccessedTime()+"<br>");
out.println("格式化后的getLastAccessedTime<br>"+new Date(session.getLastAccessedTime())+"<br>");
//返回该 session 会话被创建的时间
out.println("getCreationTime<br>"+session.getCreationTime()+"<br>");
out.println("格式化后的getCreationTime<br>"+new Date(session.getCreationTime())+"<br>");
//返回 Servlet 容器在客户端访问时保持 session 会话打开的最大时间间隔,以秒为单位。
out.println("getMaxInactiveInterval<br>"+session.getMaxInactiveInterval()+"<br>");
//返回 String 对象的枚举,String 对象包含所有绑定到该 session 会话的对象的名称
Enumeration e=session.getAttributeNames();
while(e.hasMoreElements()){
String name=(String)e.nextElement();
out.println("name<br>"+name+"<br>");
//返回在该 session 会话中具有指定名称的对象
out.println("getAttribute(name)<br>"+session.getAttribute(name)+"<br>");
} out.println("</body>");
out.println("</html>");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>TestSession</servlet-name>
<servlet-class>com.zhiqi.TestSession</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>TestSession</servlet-name>
<url-pattern>/f</url-pattern>
</servlet-mapping>
</web-app>
【模型11】Filter过滤器
@Override
public void doFilter(ServletRequest sreq, ServletResponse sresp, FilterChain fc)
throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest)sreq;
HttpSession session=request.getSession();
Object o=session.getAttribute("userNames");
String path=request.getServletPath();
if(o==null && path.indexOf("login.jsp")<0){
RequestDispatcher rd=request.getRequestDispatcher("login.jsp");
rd.forward(sreq, sresp);
}else{
fc.doFilter(sreq, sresp);
}
}
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>com.zhiqi.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
此时如果获取不到session对象啊会把主页mian.jsp转发
【模型12】HttpSessionAttributeListener监听
package com.zhiqi.listener; import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent; public class SessionAttributeListener implements HttpSessionAttributeListener { @Override
public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("添加的属性名"+httpSessionBindingEvent.getName()+"添加的属性值"+httpSessionBindingEvent.getValue()); } @Override
public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("删除的属性名"+httpSessionBindingEvent.getName()+"删除的属性值"+httpSessionBindingEvent.getValue()); } @Override
public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
// TODO Auto-generated method stub } }
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>com.zhiqi.web.Logout</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/logout</url-pattern>
</servlet-mapping>
Servlet编程-步步为营的更多相关文章
- Servlet编程
Servlet编程 1. servlet概念及相关接口简介 java Servlet是运行在web服务器或应用服务器上的程序,他是作为来自web浏览器或其他HTTP客户端的请求和HTTP服务器山的数据 ...
- Java复习10.Servlet编程
Java复习10. Servlet编程知识 20131008 前言: 之前在大三下的时候,学习了一个月的JSP和Servlet知识,但是没有什么项目经验,把JSP Web开发学习实录看了前面几张,后面 ...
- JSP编程-步步为营
[第一个JSP举例] header.jsp <%@ page language="java" contentType="text/html; charset=utf ...
- Servlet编程实例1
编程目的:使用JSP+servlet,来实现一个登陆页面,登陆成功则提示成功,登陆失败则提示失败. 编程要求:登陆页面由login.jsp负责显示,登陆成功由success.jsp负责显示,登陆失败由 ...
- Servlet 编程 请求的转发
在上篇的基础上,修改servlet *转发只能在同一应用内转发. 将forward 地址改为:youku.com 不能访问 重定向是可以访问外部应用的
- Servlet 编程 简单流程处理(重定向)
流程(来自jiekexueyuan) servlet sucess.jsp error.asp同理 运行结果:
- Servlet编程实例2
上次实验中利用HttpServletRespon.sendRedict()方法来实现页面的转跳,而这种重定向请求的方法无法传递缓存的内容. 所以为了做出改进,这次使用RequestDispatcher ...
- Servlet编程实例-servlet学习之旅(三)
LoginServlet代码: public class LoginServlet extends HttpServlet{ @Override protected void service(Http ...
- servlet编程操作
所谓servlet指:服务器处理来自Web浏览器或其他客户端的HTTP请求的服务器程序.客户端向服务器发送Http请求,经Tomcat封装处理转给Servlet容器,Servlet容器在把请求或回应交 ...
随机推荐
- 膜拜(codevs 3369)
3369 膜拜 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 神牛有很多…当然…每个同学都有自己衷心膜拜的 ...
- 考前复习(codevs 2837)
2837 考前复习 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description Aiden马上要考试了,可他 ...
- java 基本数据类型
long 是长整型,在怎么长本身也是整型,12.10的整形部分是12,当然结果是12, byte: 八位整数 -128——127,可用来节省内存的使用.short: 16位整数 -32768——32, ...
- android 实现自定义卫星菜单
看了hyman老师的视频,听起来有点迷糊,所以就想把实现卫星菜单的实现总结一下.长话短说,下面总结一下: 一.自定义ViewGroup1).自定义属性文件 属性的定义: <attr name=& ...
- MVC学习笔记---MVC的处理管线
漫步ASP.NET MVC的处理管线 ASP.NET MVC从诞生到现在已经好几个年头了,这个框架提供一种全新的开发模式,更符合web开发本质.你可以很好的使用以及个性化和扩展这个框架,但这需要你 ...
- Maven使用笔记(三)Maven的工作原理
概述 Maven是一个项目管理工具,他包含了一个项目对象模型,一组标准集合,一个项目生命周期,一个依赖管理系统和用来运行定义生命周期阶段中插件目标的逻辑. Maven是基于约定优于配置的思想来管理代码 ...
- oracle的启动过程(不分模式启动)
Oracle数据库的完整启动过程包含以下3个步骤: 简单地说,就是:启动实例-->加载数据库-->打开数据库. -------------------------------------- ...
- hdu 3746 kmp求循环节
题意就是将所给的字符串变成多个完整的循环(至少两个),然后给出最少需要添加的字符数.
- c# 作业1
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 【JUnit 报错】java.lang.IncompatibleClassChangeError
使用Junit 测试spring时候报错: java.lang.IncompatibleClassChangeError: class org.springframework.core.LocalVa ...