cookie在java中的应用案例

login.jsp

<%--
  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.jsp

<%@ 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.jsp

<%--
  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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贝西奇谈

你的鼓励将是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值