• JavaWeb-使用session机制和cookie机制改造JavaWeb基础项目


    1. 现存项目中的问题

    • 上次发表的项目实现了登录功能,但是只是有一个登录的页面,没有任何的实用性,也就是说页面仅仅是一个摆设,不登录如果知道其他页面的访问路径的话,是可以直接访问的
    • 还有一个功能尚未实现,就是实现十天内免登录

    2. 使用session机制解决未登录就访问页面的问题

        这个问题的解决比较简单,只需要两步即可;

    • 第一步:在登陆成功的时候创建一个session对象,并将用户名存入会话域中

    在这里插入图片描述

    • 第二步:在执行每一个具体操作前,获取创建的session对象,取出会话域中存储的数据,判断是否为空,不为空的情况下执行操作

    在这里插入图片描述
    在失败的情况下直接返回登录页面
    在这里插入图片描述

    3. 使用cookie机制解决十天内免登录问题

    实现免登录的思想是:

    • 保存信息:在登录成功时,并且查看前端做的标记–>复选框中是否有数据,在有数据的情况下:创建两个cookie对象,一个存储用户名,一个存储密码;并且设置存储时长和设置cookie的path属性(发送cookie的路径条件),之后就将cookie响应给浏览器;

    • 欢迎页设置:这里有两种情况,一种是保存有cookie信息,直接免登录,另一种就是需要进行登录,因此需要对欢迎页从新设置
      在这里插入图片描述
      在这里插入图片描述

      • 新创建一个Servlet,在这里首先取出有没有cookie的name属性是用户名和密码的
        • 有的话就取出进行判断,查询数据库,如果返回的结果集中有数据,说明用户名和密码正确,否则跳转到登陆界面
        • 没有的话直接跳转到登录界面
    @WebServlet("/welcome")
    public class WelcomeServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            // 获取cookie
            // 这个Cookie[]数组可能是null,如果不是null,数组的长度一定是大于0的。
            Cookie[] cookies = request.getCookies();
            String username = null;
            String password = null;
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    String name = cookie.getName();
                    if("username".equals(name)){
                        username = cookie.getValue();
                    }else if("password".equals(name)){
                        password = cookie.getValue();
                    }
                }
            }
    
            // 要在这里使用username和password变量
            if(username != null && password != null){
                // 验证用户名和密码是否正确
                Connection conn = null;
                PreparedStatement ps = null;
                ResultSet rs = null;
                boolean success = false;
                try {
                    conn = DBUtil.getConnection();
                    String sql = "select * from t_user where username = ? and password = ?";
                    ps = conn.prepareStatement(sql);
                    ps.setString(1,username);
                    ps.setString(2,password);
                    rs = ps.executeQuery();
                    if (rs.next()) {
                        // 登录成功
                        success = true;
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    DBUtil.close(conn, ps, rs);
                }
    
                if (success) {
                    // 获取session
                    HttpSession session = request.getSession();
                    session.setAttribute("username", username);
    
    
    
                    // 正确,表示登录成功
                    response.sendRedirect(request.getContextPath() + "/dept/list");
                }else{
                    // 错误,表示登录失败
                    response.sendRedirect(request.getContextPath() + "/index.jsp");
                }
            }else{
                // 跳转到登录页面
                response.sendRedirect(request.getContextPath() + "/index.jsp");
            }
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
  • 相关阅读:
    2024最新 Jenkins + Docker实战教程(八)- Jenkins实现集群并发构建
    【计算机理论基础】
    基于Matlab实现标准规划问题求解(源码+数据)
    muc和soc的区别与联系
    Go——下划线
    C# 第二章『基础语法』◆第3节:while和do...while循环语句
    CPU保护机制 —— COPP技术 (案列+配置) |||| SDN——转控分离
    B3614 【模板】栈
    简述《华为数据之道》
    MySQL笔记之一致性视图与MVCC实现
  • 原文地址:https://blog.csdn.net/weixin_44606952/article/details/126173607