Javaweb | cookie实现两周内免登录

x33g5p2x  于2021-12-06 转载在 Java  
字(4.7k)|赞(0)|评价(0)|浏览(452)
  1. 问题

2.解决思路

  1. 解决步骤

3.1 编写登录页面(lg.jsp)

3.2 编写一个登录成功页面(success.jsp)

3.3 编写登录失败页面(fail.html)

3.4 写一个cookie的类用来存储从页面获取的用户信息(Login.java)

3.5 在登录页面(lg.jsp)添加检索Cookie是否存在

  1. 运行结果

1. 问题

在用户登陆界面中,输入账号和密码,登陆成功时,进入欢迎页面。在登录时,如果勾选了两周内免登录,则在下次登录时,可以不用输入密码,直接跳转到成功页面,从而实现免登录功能。

2.解决思路

当用户登陆时勾选了免登录按钮,在cookie的处理逻辑中就会将从登录页面中获取到的用户信息存储在cookie中,同时可以设置cookie的有效时间等。当用户在cookie有效时间内再次登录,在登陆页面就会获取cookie,查看其中是否有之前储存的数据,如果有就会直接跳转到成功页面。

3. 解决步骤

3.1 编写登录页面(lg.jsp)

<%@ page import="java.io.PrintWriter" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %><%--
  Created by IntelliJ IDEA.
  User: dell
  Date: 2021/11/4
  Time: 21:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>登陆页面</title>
    <style>
        .lg {
            margin-top: 100px;
        }

        body {
            background: aliceblue;
        }
    </style>
</head>
<body>
<div class="lg">
    <form action="lg" method="post">
        <table border="1" width="30%" align="center">
            <caption>用户登录</caption>
            <tr>
                <td><label for="username">用户名:</label></td>
                <td><input type="text" name="username" placeholder="请输入用户名" id="username" value="<%=username%>"></td>
            </tr>
            <tr>
                <td><label for="password">密码:</label></td>
                <td><input type="password" name="password" placeholder="请输入密码" id="password" value="<%=password%>"></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input name="twoDays" type="checkbox" value="ok">两天内免登陆</td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="登陆"></td>
            </tr>
        </table>
    </form>
</div>
</body>
</html>

3.2 编写一个登录成功页面(success.jsp)

<%@ page import="java.io.PrintWriter" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %><%--
  Created by IntelliJ IDEA.
  User: dell
  Date: 2021/11/4
  Time: 21:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>登陆页面</title>
    <style>
        body {
            background: aliceblue;
        }
    </style>
</head>
<body>
登陆成功!!
</body>
</html>

3.3 编写登录失败页面(fail.html)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>fail</title>
</head>
<body>
用户名或密码错误,请
<a href="lg.jsp">重新登陆</a>
</body>
</html>

3.4 写一个cookie的类用来存储从页面获取的用户信息(Login.java)

@WebServlet("/lg")
public class Login extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String twoDays = request.getParameter("twoDays");
        PrintWriter out = response.getWriter();

        //查看用户名和密码是否正确,我这里对用户名和密码没有强用要求,只要输入了就行,可以自己改
        if (username != null && password != null) {
            if ("ok".equals(twoDays)) {
                //将用户信息存储到cookie中,并且关联路径和设置有效时间
                Cookie cookie1 = new Cookie("username", username);
                Cookie cookie2 = new Cookie("password", password);
                cookie1.setPath("/");
                cookie2.setPath("/");
                cookie1.setMaxAge(10);//10秒
                cookie2.setMaxAge(10);
                //服务器发送cookie给客户端浏览器
                response.addCookie(cookie1);
                response.addCookie(cookie2);
            }
            //跳转到主页
            response.sendRedirect("success.jsp");
        } else {
            //跳转到登陆失败页面
            response.sendRedirect("fail.html");
        }
    }
}

3.5 在登录页面(lg.jsp)添加检索Cookie是否存在

<%
    //获取cookie
    Cookie[] cookies = request.getCookies();
    //取出cookie中的值
    String username = "";
    String password = "";
    if (cookies != null && cookies.length > 0) {
        for (Cookie cookie : cookies) {
            String cookieName = cookie.getName();
            if ("username".equalsIgnoreCase(cookieName)) {
                username = cookie.getValue();
            }
            if ("password".equalsIgnoreCase(cookieName)) {
                password = cookie.getValue();
            }
            if ("username".equalsIgnoreCase(cookieName)){
                request.getRequestDispatcher("success.jsp").forward(request, response);
            }
        }
    }
%>

4. 运行结果

跳转到成功页面


** 再次访问登陆界面时,就会跳转到成功页面。**

如果有错误或者有疑问,欢迎一起交流~~

相关文章