码迷,mamicode.com
首页 > 其他好文 > 详细

ServletContext对象应用——三天免登录

时间:2019-09-21 23:07:59      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:递增   bsp   访问   rom   次数   from   login   account   ima   

1、用到的知识点:

(1)Cookie

(2)Session

(3)ServletContext

技术图片

 

 

 

要想实现三天免登录记录网站访问次数的功能,只有Cookie和Session技术是不行的,因为Cookie实现的的是免登录功能。通过创建Cookie,生成了Cookie文件,在下次登录时可以直接读取Cookie中的用户信息,不用重新输入账户和密码。Session的运用解决的是不同请求的数据共享问题,即创建了Session后,在访问不同的页面的时候,可以通过Request对象获取Session的值。

要想记录页面访问的次数,要用到ServletContext技术,可以解决不同用户的数据共享问题。即ServletContext中的值是所有用户共享的。可以获取或修改值。

2、过程分析:

第一次登录(没有Sessio和Cookie):

技术图片

 

 

 技术图片

 

 

 需要进行手动登录。

技术图片

 

 

 

首次登录后(Cookie未过期之前)登录:

技术图片

MainServlet的运行结果:

 

 

 技术图片

 

 

 因为Session的运用在CookieServlet中存储的Session中的数据,在MainSewrvlet中依旧能够调用。

由于SertvletContext的运用,浏览量会递增,不会因为浏览器、服务器的关闭而造成数据从0开始增加。

3、代码分析:

CookieServlet:

 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);
                            ServletContext servletContext = this.getServletContext();//获取ServletContext对象
                            if((servletContext.getAttribute("contextNum"))!=null) {
                                int num=Integer.parseInt(servletContext.getAttribute("contextNum").toString());
                                num++;
                                servletContext.setAttribute("contextNum",num);//将int类型的num储存到contextNum
                            }else{
                              servletContext.setAttribute("num",1);
                            }

                           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和ServletContext,无Cookie或校验不成功则请求转发到主页面。

MainServlet:

  login log= (login) request.getSession().getAttribute("login");
        int num=(int)this.getServletContext().getAttribute("contextNum");

通过Session和ServletContext获取CookieServlet中的数据。

LogServlet:

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);
                Cookie cookie=new Cookie(account,password);
                cookie.setPath("/Servlet_login_war_exploded/cookie");
                cookie.setMaxAge(3*24*3600);
                response.addCookie(cookie);
            }
            else{
                response.getWriter().write("wrong");
            }

是在无Cookie或数据库校验失败的情况下调用的,生成Cookie。

4、问题分析(类型转换错误):

错误代码:

int num=(int) this.getServletContext().getAttribute("contextNum");

原因:this.getServletContext().getAttribute("contextNum")为Object类型,需要先转换为String类型,再转换为int类型:

int num=Integer.parseInt(servletContext.getAttribute("contextNum").toString());

 

ServletContext对象应用——三天免登录

标签:递增   bsp   访问   rom   次数   from   login   account   ima   

原文地址:https://www.cnblogs.com/zhai1997/p/11564894.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!