zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    【独家】K8S漏洞报告 | 近期bug fix解读
    idou老师教你学Istio 29:Envoy启动流程
    idou老师教你学Istio 28:istio-proxy check 的缓存
    idou老师教你学Istio :5分钟简析Istio异常检测
    idou老师教你学Istio 27:解读Mixer Report流程
    idou老师教你学Istio 26:如何使用Grafana进行可视化监控
    idou老师教你学Istio 25:如何用istio实现监控和日志采集
    idou老师教你学Istio 24:如何在Istio使用Prometheus进行监控
    idou老师教你学Istio 23 : 如何用 Istio 实现速率限制
    idou老师教你学Istio 22 : 如何用istio实现调用链跟踪
  • 原文地址:https://www.cnblogs.com/1234ply/p/10654231.html
Copyright © 2011-2022 走看看