Cookie实现记住用户名密码

前言

本文提供使用cookie实现记住用户名、密码的代码实例、难点注释。
文末有完整项目代码下载地址。

实现界面

初始登录界面
在这里插入图片描述
输入:
张三
123456
点击登录之后跳转界面:
在这里插入图片描述
可查看用户信息:
在这里插入图片描述
关闭所有页面,重新打开登录页面后保留上一次登录信息:
在这里插入图片描述

实现代码

login.jsp

<%@ page contentType="text/html;charset=UTF-8" import="java.util.*,java.net.*" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
 
%>
<html>
<head>
    <base href="<%=basePath%>">
    <title>login page</title>
</head>
<body>
<h1>用户登录</h1>
<hr/>
<%
    request.setCharacterEncoding("utf-8");
    String userName = "";
    String passWord = "";
    //以下表示找到已存在的Cookie对象
    Cookie[] cookies = request.getCookies();
    if (cookies != null && cookies.length > 0) {
        for (Cookie c : cookies) {
            if (c.getName().equals("usernameC")) {
                userName = URLDecoder.decode(c.getValue(), "utf-8");//使用URLDecoder解码
            }
            if (c.getName().equals("passwordC")) {
                passWord = URLDecoder.decode(c.getValue(), "utf-8");
            }
        }
    }
%>
<form name="loginForm" 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"><input type="checkbox" name="isUseCookie" checked="checked"/>十天内记住我的登录状态</td>
        </tr>
        <tr>
            <td colspan="2" align="center"><input type="submit" value="登录"/><input type="reset" value="取消"></td>
        </tr>
    </table>
</form>
</body>
</html>

dologin.jsp

<%@ page import="java.text.CollationKey" %>
<%@ page import="java.net.*" %>
<%@ page import="sun.nio.cs.UTF_32" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
    <base href="<%=basePath%>">
    <title>dologin page</title>
</head>
<body>
<h1>登录成功!</h1>
<hr>
<br>
<br>
<br>
<%
    //作用是用指定的编码集去覆盖request对象中的默认的"ISO-8859-1"编码集,
    // 这样request.getParameter("")方法就会用新的编码集去解码,
    // 但是这个方法有一个条件,就是必须在第一次使用request时就要调用这个方法来设置编码集,否则该方法就会无效。
    //这里设置的是编码,之后在user中取值还需要解码(或者转码):URLDecoder.decode(c.getValue(),"utf-8");//使用URLDecoder解码
    request.setCharacterEncoding("utf-8");
    //首先判断用户是否选择了记住登录状态
    String[] isUseCookie = request.getParameterValues("isUseCookie");
    if (isUseCookie != null && isUseCookie.length > 0) {
        //把用户名和密码保存在Cookie对象中
        String username = URLEncoder.encode(request.getParameter("username"), "utf-8");
        //使用URLEncoder解决中文报错问题,在net包中
        String password = URLEncoder.encode(request.getParameter("password"), "utf-8");
        Cookie usernameCookie = new Cookie("usernameC", username);
        Cookie passwordCookie = new Cookie("passwordC", password);
        //保存上面两个对象
        usernameCookie.setMaxAge(864000);
        passwordCookie.setMaxAge(864000);//该Cookie对象保存多少秒(在这里是设置生存期限为十天)
        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().equals("usernameC") || c.getName().equals("passwordC")) {
                    c.setMaxAge(0);//设置Cookie失效
                    response.addCookie(c);//重新保存
                }
            }
        }
    }
%>
<a href="users.jsp" target="_blank">查看用户信息</a>
</body>
</html>

users.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
    <base href="<%=basePath%>">
    <title>用户信息page</title>
</head>
<body>
<h1>用户信息</h1>
<hr>
<br>
<br>
<br>
<%
    request.setCharacterEncoding("utf-8");
    String userName = "";
    String passWord = "";
    //以下表示找到已存在的Cookie对象
    Cookie[] cookies = request.getCookies();
    if (cookies != null && cookies.length > 0) {
        for (Cookie c : cookies) {
            if (c.getName().equals("usernameC")) {
                userName = URLDecoder.decode(c.getValue(), "utf-8");//使用URLDecoder解码
            }
            if (c.getName().equals("passwordC")) {
                passWord = URLDecoder.decode(c.getValue(), "utf-8");//使用URLDecoder解码
            }
        }
    }
%>
用户名:<%=userName%><br>
密码:<%=passWord%><br>
</body>
</html>

博主使用(intellij idea+tomcat)
github项目地址:https://github.com/StathamWYJ/cookieDemo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值