实现在登陆时点击记住我下次登录可以直接访问到

首先写一个简陋的登陆页面 ----有记住我的复选框

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="do_login.jsp" method="post">
    用户名:<input type="text" name="username"><br>
    密码 <input type="password" name="password"><br>
    <input type="checkbox" name="rea" value="0">记住我<br/>
    <input type="submit" value="登录">

</form>
</body>
</html>

再写do_login.jsp是逻辑判断检查用户是否勾选了"记住我".并设置有效期 ---判断登录成功与否并体跳转到不同的页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    request.setCharacterEncoding("utf-8");
    String username = request.getParameter("username").trim();
    String password = request.getParameter("password").trim();
    String[] dians = request.getParameterValues("rea");
    boolean ia=false;
    if (dians!=null && dians.length==1){
        ia=true;
    }
    if ("curry".equals(username) && "30".equals(password)){
        //设置用户登陆消息
        session.setAttribute("username",username);
        //设置session过期时间
        session.setMaxInactiveInterval(60*60);
        //检查用户是否自动登录
        if (ia){
            //如果选择自动登录,则设置Cookie并发送给客户端
            Cookie cookie1=new Cookie("username",username);
            Cookie cookie2=new Cookie("password",password);
            //设置有效期为1天
            cookie1.setMaxAge(60*60*24);
            cookie2.setMaxAge(60*60*24);
            response.addCookie(cookie1);
            response.addCookie(cookie2);
        }else{
            //如果没有选择自动登录 则删除Cookie
            Cookie[] cookies = request.getCookies();
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("username")){
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                }
                if (cookie.getName().equals("password")){
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                }
            }
        }
        //登陆成功后,打开用户首页
        request.getRequestDispatcher("home.jsp").forward(request,response);

    }else {
        //登陆失败,提示重新登陆
        response.sendRedirect("login.jsp");
    }
%>

</body>
</html>

如果登录失败,再次跳转到登陆也页面,登陆成功跳转到home.jsp页面:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    request.setCharacterEncoding("utf-8");
    String username = request.getParameter("username").trim();
    String password = request.getParameter("password").trim();
    String[] dians = request.getParameterValues("rea");
    boolean ia=false;
    if (dians!=null && dians.length==1){
        ia=true;
    }
    if ("curry".equals(username) && "30".equals(password)){
        //设置用户登陆消息
        session.setAttribute("username",username);
        //设置session过期时间
        session.setMaxInactiveInterval(60*60);
        //检查用户是否自动登录
        if (ia){
            //如果选择自动登录,则设置Cookie并发送给客户端
            Cookie cookie1=new Cookie("username",username);
            Cookie cookie2=new Cookie("password",password);
            //设置有效期为1天
            cookie1.setMaxAge(60*60*24);
            cookie2.setMaxAge(60*60*24);
            response.addCookie(cookie1);
            response.addCookie(cookie2);
        }else{
            //如果没有选择自动登录 则删除Cookie
            Cookie[] cookies = request.getCookies();
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("username")){
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                }
                if (cookie.getName().equals("password")){
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                }
            }
        }
        //登陆成功后,打开用户首页
        request.getRequestDispatcher("home.jsp").forward(request,response);

    }else {
        //登陆失败,提示重新登陆
        response.sendRedirect("login.jsp");
    }
%>

</body>
</html>

注意:如果登陆时勾选了记住我 下一次可以直接访问home.jsp但是如果访问do_login.jsp会报错500,报错内容是空指针异常。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值