cookie的使用
一.过滤器
1 import java.io.IOException; 2 import javax.servlet.FilterChain; 3 import javax.servlet.FilterConfig; 4 import javax.servlet.ServletException; 5 import javax.servlet.ServletRequest; 6 import javax.servlet.ServletResponse; 7 import javax.servlet.annotation.WebFilter; 8 import javax.servlet.http.Cookie; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 /** 13 * Servlet Filter implementation class Filter 14 */ 15 @WebFilter("/*") 16 public class Filter implements javax.servlet.Filter { 17 18 /** 19 * Default constructor. 20 */ 21 public Filter() { 22 // TODO Auto-generated constructor stub 23 } 24 25 /** 26 * @see Filter#destroy() 27 */ 28 public void destroy() { 29 // TODO Auto-generated method stub 30 } 31 32 /** 33 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) 34 */ 35 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 36 // TODO Auto-generated method stub 37 38 // 将请求转换成HttpServletRequest请求 39 HttpServletRequest httpRequest=(HttpServletRequest)request; 40 // 将响应转换成HttpServletResponse响应 41 HttpServletResponse httpResponse = (HttpServletResponse)response; 42 //截取用户的请求地址 43 String from =httpRequest.getServletPath(); 44 System.out.println(from); 45 if(from.equals("/login.html")) { 46 //获取本站在客户端上保留的所有的cookie 47 Cookie[] cookies=httpRequest.getCookies(); 48 if(cookies!=null) { 49 //遍历cookie数组 50 51 httpResponse.sendRedirect("cookieSave"); 52 } 53 // 将保存在cookie中的用户名和密码保存在request 54 /*httpRequest.setAttribute("username",username); 55 httpRequest.setAttribute("password", password);*/ 56 57 // 放行请求 58 chain.doFilter(request, response); 59 } 60 else { 61 62 // 用户请求的地址是servlet,直接放行请求 63 64 chain.doFilter(request, response); 65 66 } 67 } 68 69 /** 70 * @see Filter#init(FilterConfig) 71 */ 72 public void init(FilterConfig fConfig) throws ServletException { 73 // TODO Auto-generated method stub 74 //System.err.println("filter init"); 75 } 76 77 }
二.servlet
1 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 2 // TODO Auto-generated method stub 3 //response.getWriter().append("Served at: ").append(request.getContextPath()); 4 response.setContentType("text/html; charset=utf-8"); 5 request.setCharacterEncoding("utf-8"); 6 String un=request.getParameter("username"); 7 String pwd=request.getParameter("password"); 8 String rem=request.getParameter("remember"); 9 Cookie[] cookies=request.getCookies(); 10 11 if(cookies!=null) { 12 for (Cookie cookie : cookies) { //遍历cookie对象 13 if (cookie.getName().equals("username")) 14 {// 已登录且记住密码 跳转到首页 15 un = cookie.getValue(); 16 //System.out.println(un); 17 } 18 if(cookie.getName().equals("password")) { 19 pwd = cookie.getValue(); 20 //System.out.println(pwd); 21 } 22 // System.out.println(cookie.getName()); 23 // cookie.setMaxAge(0); 24 // cookie.setPath("/"); 25 // cookie.setValue(""); 26 // response.addCookie(cookie); 27 28 } 29 } 30 /*Cookie[] cookies2=request.getCookies(); 31 for (Cookie cookie : cookies2) { 32 if (cookies2!=null) { 33 34 System.out.println("*********"); 35 } 36 }*/ 37 38 //判断账号密码是否正确 39 if(un.equals("tom") && pwd.equals("123") && rem!=null) {//记住密码 40 Cookie cookie=new Cookie("username", "tom"); 41 Cookie cookie2 = new Cookie("password", "123");//创建一个新的Cooike 42 cookie.setMaxAge(30*24*60*60); //设置最大的保存时间 43 cookie2.setMaxAge(30*24*60*60); 44 response.addCookie(cookie); 45 response.addCookie(cookie2);//添加cookie 到服务器端 46 response.sendRedirect("shouye.html"); 47 } 48 49 else if(un.equals("tom") && pwd.equals("123")) { //未记住密码 50 response.sendRedirect("shouye.html"); 51 } 52 else { 53 log("用户名或密码错误"); 54 response.sendRedirect("login.html"); 55 } 56 57 }