Session的应用——三天免登录

Posted zhai1997

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Session的应用——三天免登录相关的知识,希望对你有一定的参考价值。

1、使用Cookie实现的登录的不足:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
        Connection con=null;
        login log= null;
        String account=request.getParameter("username");//获得表单数据只在此Servlet起作用
        String password=request.getParameter("password");
        Cookie cookie=new Cookie(account,password);
        cookie.setPath("/Servlet_login_war_exploded/cookie");
        cookie.setMaxAge(3*24*3600);
        response.addCookie(cookie);
        try 
            con=C3p0Utils.getConnection();
            QueryRunner qr = new QueryRunner();
            String sql = "Select * from login where account=? and password=?";
            Object[] select = account,password;
            log = qr.query(con, sql, new BeanHandler<login>((login.class)), select);
            if(log!=null)

                response.getWriter().write("nihao"+account);
            
            else

                response.getWriter().write("wrong");
            

        
        catch (SQLException e) 
            throw new RuntimeException(e);
        
    

 

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
        response.setContentType("text/html; charset=utf-8");//设置浏览器编码格式
        response.getWriter().write("<html>");
        response.getWriter().write("<head>");
        response.getWriter().write("<title>");
        response.getWriter().write("Login");
        response.getWriter().write("</title>");
        response.getWriter().write("</head>");
        response.getWriter().write("<body bgcolor=\\"aqua\\">");
        response.getWriter().write("<center>");
        response.getWriter().write("<h3>");
        response.getWriter().write("欢迎你"+request.getParameter("account"));
        response.getWriter().write("</h3>");
        response.getWriter().write("</center>");
        response.getWriter().write("</body>");
    

使用Cookie虽然实现了三天免登陆的基础功能,但是,如果在用Cookie登录成功后需要重定向到另外一个Cookie时,request获取的值在重定向的Servlet中已经不再起作用了。访问的结果只能是空值。

技术图片

 

 2、使用Session的请求共享功能,实现在不同的Servlet跳转过程中依旧能够通过request获得用户信息。

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");//设置浏览器编码格式
        Cookie[] cookies=request.getCookies();
        Connection con=null;
        login log= null;
        int successNum=0;
        try 
            con= C3p0Utils.getConnection();
            QueryRunner qr = new QueryRunner();
            String sql = "Select * from login";
            List<login> list = qr.query(con, sql, new BeanListHandler<login>((login.class)));
            if(cookies!=null) //验证数据库中是否有与Cookie对应的用户
                for (int i = 0; i < list.size(); i++) 
                    log= list.get(i);
                for (Cookie cookie : cookies) 
                        if((log.getAccount().equals(cookie.getName()))&&(log.getPassword().equals(cookie.getValue())))
                            HttpSession httpSession=request.getSession();
                            httpSession.setAttribute("login",log);
                           successNum++;
                        
                
                
                if(successNum>=1)

                    response.sendRedirect("/Servlet_login_war_exploded/main");//重定向
                
                else
                    request.getRequestDispatcher("page").forward(request,response);
                

            
            else
                request.getRequestDispatcher("page").forward(request,response);//请求转发
            
        
        catch (SQLException e) 
            throw new RuntimeException(e);
        
    

在通过Cookie登录成功后,创建了Session,对Session进行了赋值,而在重定向到MainServlet后,可以冲Session中获取值。虽然在不同的Servlet中,是不同的请求,但是依旧能够通过Session获取值。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
        login log= (login) request.getSession().getAttribute("login");
        response.setContentType("text/html; charset=utf-8");//设置浏览器编码格式
        response.getWriter().write("<html>");
        response.getWriter().write("<head>");
        response.getWriter().write("<title>");
        response.getWriter().write("Login");
        response.getWriter().write("</title>");
        response.getWriter().write("</head>");
        response.getWriter().write("<body bgcolor=\\"aqua\\">");
        response.getWriter().write("<center>");
        response.getWriter().write("<h3>");
        response.getWriter().write("欢迎你"+log.getAccount());
        response.getWriter().write("</h3>");
        response.getWriter().write("</center>");
        response.getWriter().write("</body>");
    

技术图片 

以上是关于Session的应用——三天免登录的主要内容,如果未能解决你的问题,请参考以下文章

利用Session实现三天免登陆

7天免登陆

用户登录之7天免登陆(垃圾回收机制)

13 Servlet——session案例2:用户登录主页显示用户名和注销登录

Cookie和Session

Cookie和Session使用