cookie的使用

一.过滤器

import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet Filter implementation class Filter
 */
@WebFilter("/*")
public class Filter implements javax.servlet.Filter {

    /**
     * Default constructor. 
     */
    public Filter() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Filter#destroy()
     */
    public void destroy() {
        // TODO Auto-generated method stub
    }

    /**
     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // TODO Auto-generated method stub
        
        // 将请求转换成HttpServletRequest请求
        HttpServletRequest httpRequest=(HttpServletRequest)request;
        // 将响应转换成HttpServletResponse响应
        HttpServletResponse httpResponse = (HttpServletResponse)response;
        //截取用户的请求地址
        String from =httpRequest.getServletPath();
        System.out.println(from);
        if(from.equals("/login.html")) {
            //获取本站在客户端上保留的所有的cookie
            Cookie[] cookies=httpRequest.getCookies();
            if(cookies!=null) {
                //遍历cookie数组
                
                httpResponse.sendRedirect("cookieSave");
            }
            // 将保存在cookie中的用户名和密码保存在request
            /*httpRequest.setAttribute("username",username);
            httpRequest.setAttribute("password", password);*/
            
            // 放行请求
            chain.doFilter(request, response);
        }
        else {

            // 用户请求的地址是servlet,直接放行请求

            chain.doFilter(request, response);

        }
    }

    /**
     * @see Filter#init(FilterConfig)
     */
    public void init(FilterConfig fConfig) throws ServletException {
        // TODO Auto-generated method stub
        //System.err.println("filter init");
    }

}
View Code

二.servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //response.getWriter().append("Served at: ").append(request.getContextPath());
        response.setContentType("text/html; charset=utf-8");
        request.setCharacterEncoding("utf-8");
        String un=request.getParameter("username");
        String pwd=request.getParameter("password");
        String rem=request.getParameter("remember");
        Cookie[] cookies=request.getCookies();    

        if(cookies!=null) {
            for (Cookie cookie : cookies) {                    //遍历cookie对象
                if (cookie.getName().equals("username"))
                {//  已登录且记住密码 跳转到首页
                    un = cookie.getValue();
                    //System.out.println(un);
                }
                if(cookie.getName().equals("password")) {
                    pwd = cookie.getValue();
                    //System.out.println(pwd);
                }
//                System.out.println(cookie.getName());
//                 cookie.setMaxAge(0);
//                 cookie.setPath("/");
//                 cookie.setValue("");
//                 response.addCookie(cookie);

            }
        }
        /*Cookie[] cookies2=request.getCookies();    
        for (Cookie cookie : cookies2) {
            if (cookies2!=null) {
                
                System.out.println("*********");
            }
        }*/
        
        //判断账号密码是否正确
            if(un.equals("tom") && pwd.equals("123") && rem!=null) {//记住密码
                Cookie cookie=new Cookie("username", "tom");    
                Cookie cookie2 = new Cookie("password", "123");//创建一个新的Cooike
                cookie.setMaxAge(30*24*60*60);                    //设置最大的保存时间    
                cookie2.setMaxAge(30*24*60*60);
                response.addCookie(cookie);                
                response.addCookie(cookie2);//添加cookie 到服务器端
                response.sendRedirect("shouye.html");            
            }
            
            else if(un.equals("tom") && pwd.equals("123")) {        //未记住密码
                response.sendRedirect("shouye.html");
            }
            else {
                 log("用户名或密码错误");
                 response.sendRedirect("login.html");
            }
        
        }
View Code

 

posted @ 2019-04-04 12:55  可爱善良有素质  阅读(153)  评论(0编辑  收藏  举报