cookie在java中的应用案例

login.jsphtml

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/5/7/007
  Time: 14:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
<body>
<%
    String username="";
    String password="";
    //接收cookie对象中的值
    Cookie[] cookies = request.getCookies();
    if(cookies!=null && cookies.length>0){
        for (Cookie c:cookies) {
            if(c.getName().equalsIgnoreCase("username")){
                username=c.getValue();
            }
            if(c.getName().equalsIgnoreCase("password")){
                password=c.getValue();
            }
        }
    }
%>
    <center>
        <h1>用户登录</h1>
        <hr>
        <form action="doLogin.jsp" method="post">
            <table>
                <tr>
                    <td>用户名:</td>
                    <td><input type="text" name="username" value="<%=username%>"/></td>
                </tr>
                <tr>
                    <td>密码:</td>
                    <td><input type="password" name="password" value="<%=password%>"/></td>
                </tr>
                <tr>
                    <td colspan="2" align="center"><input type="checkbox" checked name="isUseCookie">
                    记住用户名和密码</td>
                </tr>
                <tr>
                    <td colspan="2" align="center"><input type="submit" value="登录"></td>
                </tr>
            </table>
        </form>
    </center>
</body>
</html>

doLogin.jspjava

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>doLogin</title>
</head>
<body>
<%
    //首先判断用户是否选择了记住登录状态
    String[] isUseCookie = request.getParameterValues("isUseCookie");
    if(isUseCookie!=null && isUseCookie.length>0){
        //接收用户名和密码参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //把用户名和密码保存到cookie对象中
        Cookie usernameCookie=new Cookie("username", username);
        Cookie passwordCookie=new Cookie("password", password);
        //设置cookie的保存时间
        usernameCookie.setMaxAge(86400);
        passwordCookie.setMaxAge(86400);
        //响应数据到浏览器
        response.addCookie(usernameCookie);
        response.addCookie(passwordCookie);
    }
    else{//设置cookie对象失效
        //接收cookie中的值
        Cookie[] cookies = request.getCookies();
        if(cookies!=null && cookies.length>0){
            for(Cookie c:cookies){
                if(c.getName().equalsIgnoreCase("username")||c.getName().equalsIgnoreCase("password")){
                    c.setMaxAge(0);//失效
                    response.addCookie(c);//从新保存cookie
                }
            }
        }
    }
%>
<a href="user.jsp" target="_blank">查看登录信息</a>
</body>
</html>

user.jspweb

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/5/7/007
  Time: 15:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>信息</title>
</head>
<body>
<%
    String username="";
    String password="";
    //接收cookie对象中的值
    Cookie[] cookies = request.getCookies();
    if(cookies!=null && cookies.length>0){
        for (Cookie c:cookies) {
            if(c.getName().equalsIgnoreCase("username")){
                username=c.getValue();
            }
            if(c.getName().equalsIgnoreCase("password")){
                password=c.getValue();
            }
        }
    }
%>
<center >
    <h1>用户信息</h1>
    用户名:<%=username%> <br>
    密码:<%=password%>
</center>
</body>
</html>