CookieDemo

一曲冷凌霜 提交于 2020-01-29 08:51:22

练习1、使用Cookie简化用户登录
需求说明:
用户首次登录时要求输入用户名和密码
登录成功后保存用户的登录状态
设置cookie的有效期为5分钟
在cookie有效期内,可无需登录直接进入欢迎页面
提示:
如果用户名和密码正确,创建Cookie保存信息
使用setMaxAge()方法设置Cookie的有效期
页面访问时首先读取Cookie进行用户信息判断
在这里插入图片描述
1.login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>

<html>
<head>
    <title>用户登录</title>
</head>
<body>
<h3 style="text-align: center">用户登录</h3>
<form action="do_login.jsp" method="post">
    <table border="1" cellpadding="10" style="margin: 0px auto">
        <tr>
            <td align="center">用户名</td>
            <td><input id="uname" type="text" name="username"/></td>
        </tr>
        <tr>
            <td align="center">&nbsp;</td>
            <td><input id="upwd" type="password" name="password"/></td>
        </tr>
        <tr align="center">
            <td colspan="2">
                <input id="saveuname" type="checkbox" name="save" value="记住用户名"/>记住用户名
                <input id="saveupwd" type="checkbox" name="save" value="记住密码" onclick="setchkuname()"/>记住密码
            </td>
        </tr>
        <tr align="center">
            <td colspan="2">
                <input type="submit" value="登录"/>
                <input type="reset" value="重置"/>
            </td>
        </tr>
    </table>
</form>
<%
    String uname = "";
    String upwd = "";
    String saveuname = "";
    String saveupwd = "";
    Cookie[] cookies = request.getCookies();
    for (Cookie cookie: cookies) {
        if (cookie.getName().equals("uname")) {
            uname = cookie.getValue();
        }
        if (cookie.getName().equals("upwd")) {
            upwd = cookie.getValue();
        }
        if (cookie.getName().equals("saveuname")) {
            saveuname = cookie.getValue();
        }
        if (cookie.getName().equals("saveupwd")) {
            saveupwd = cookie.getValue();
        }
    }

    String errMsg = (String) session.getAttribute("errMsg");
    if (errMsg != null){
        errMsg = new String(errMsg.getBytes("iso-8859-1"),"utf-8");
        out.print("<script>alert('" + errMsg + "')</script>");
    }
%>
<script type="text/javascript">
    // 显示用户做出的是否记住用户名的选择
    var chkSaveUname = document.getElementById("saveuname");
    var saveuname = "<%= saveuname %>";
    if (saveuname == "yes") {
        chkSaveUname.checked = true;
    }

    // 显示用户做出的是否记住密码的选择
    var chkSaveUpwd = document.getElementById("saveupwd");
    var saveupwd = "<%= saveupwd %>";
    if (saveupwd == "yes") {
        chkSaveUpwd.checked = true;
    }

    var txtUname = document.getElementById("uname");
    var txtUpwd = document.getElementById("upwd");

    // 根据用户做出的选择,决定是否自动填充用户名或密码
    if (chkSaveUname.checked) {
        txtUname.value = "<%= new String(uname.getBytes("iso-8859-1"), "utf-8") %>";
    }
    if (chkSaveUpwd.checked) {
        txtUpwd.value = "<%= upwd %>";
    }

    /**
     * 选择【记住密码】复选框,自动会选择【记住用户名】复选框
     */
    function setchkuname() {
        if (chkSaveUpwd.checked) {
            chkSaveUname.checked = true;
        }
    }
</script>
</body>
</html>

2.do_login.jsp

<%
    // 获取表单提交的数据
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    String[] choice = request.getParameterValues("save");
    // 判断用户是否登录成功
    if (username.equals("无心剑") && password.equals("903213")) {
        // 创建Cookie对象
        Cookie uname = new Cookie("uname", username.trim());
        Cookie upwd = new Cookie("upwd", password.trim());
        Cookie saveuname = new Cookie("saveuname", "no");
        Cookie saveupwd = new Cookie("saveupwd", "no");
        if (choice != null) {
            if (choice.length == 2) {
                saveuname.setValue("yes");
                saveupwd.setValue("yes");
            } else {
                saveuname.setValue("yes");
            }
        }
        // 将Cookie对象写入客户端
        //cookie设置有效时间30秒
        uname.setMaxAge(30);
        response.addCookie(uname);
        upwd.setMaxAge(30);
        response.addCookie(upwd);
        response.addCookie(saveuname);
        response.addCookie(saveupwd);
        // 采用重定向,跳转到登录成功页面
        response.sendRedirect("success.jsp");
    } else {
        Cookie saveuname = new Cookie("saveuname","no");
        Cookie saveupwd = new Cookie("saveupwd","no");
        saveuname.setMaxAge(5*60);
        response.addCookie(saveuname);
        saveupwd.setMaxAge(5*60);
        response.addCookie(saveupwd);

        session.setAttribute("errMsg","用户名或密码错误,请重新登录!");
        // 采用重定向,跳转到登录页面
        response.sendRedirect("login.jsp");
    }
%>

3.success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录成功</title>
</head>
<body>
<%
    String uname = "";
    Cookie[] cookies = request.getCookies();
    for (Cookie cookie:cookies){
        if (cookie.getName().equals("uname")){
            uname = cookie.getValue();
        }
    }
%>
<h3><%= new String(uname.getBytes("iso-8859-1"),"utf-8")%>,登录成功!</h3>
</body>
</html>

在这里插入图片描述

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!