J2EE的cookie模拟代码

index.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String username = "", password = "";
    String remember1 = null, remember2 = null;
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("loginname")) {
                username = cookie.getValue();
            }
            if (cookie.getName().equals("loginpwd")) {
                password = cookie.getValue();
            }
            if (cookie.getName().equals("remember1")) {
                remember1 = cookie.getValue();
            }
            if (cookie.getName().equals("remember2")) {
                remember2 = cookie.getValue();
            }
        }
    }
    for (Cookie cookie : cookies) {
        response.addCookie(cookie);//服务端给客户端的cookie
    }
%>
<%--先检查一遍cookie,如果有cookie,则添加到浏览器里面。--%>
<%--然后表单的作用就是让用户填写账号密码,提交了之后页面跳转到login.jsp里面--%>
<form method="post" action="login.jsp">
    <table align="center">
        <tr>
            <td>用户名称:</td>
            <td><input type="text" name="username" value="<%=username%>" id="username"></td>
        </tr>
        <tr>
            <td align="left">密码:</td>
            <td><input type="password" name="pwd" value="<%=password%>" id="pwd"></td>
        </tr>
        <tr>
            <td align="right"><input type="checkbox" name="remember1" <%=remember1%>></td>
            <td>自动记录名称</td>
        </tr>
        <tr>
            <td align="right"><input type="checkbox" name="remember2" <%=remember2%>></td>
            <td>自动记录密码</td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <input type="submit" value="登录">
                <input type="reset" value="重置">
            </td>
        </tr>
    </table>
</form>
</body>
</html>

login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
//    跳转过来之后,检查账号密码是否正确,如果正确了,就显示登陆成功,返回cookie到浏览器那里,
//    并且显示一个跳转的链接,如果密码不对就重新跳转回到index.jsp
    String username = request.getParameter("username");//获取用户名
    String password = request.getParameter("pwd");//获取密码
    String remember1 = request.getParameter("remember1");//得到复选框☑️
    String remember2 = request.getParameter("remember2");//得到复选框☑️
    if (username.equals("abc") && password.equals("123")) {
        out.println("登录成功<br/>");
        if (remember1 != null && remember1.equals("on")) { // 点了✅就是on,没点✅就是null
            //rem1必须点击了之后才能不是null
            Cookie mycookie1 = new Cookie("loginname", username);
            Cookie mycookie3 = new Cookie("remember1", "checked");

            response.addCookie(mycookie1);
            response.addCookie(mycookie3);
        }
        if (remember2 != null && remember2.equals("on")) {
            Cookie mycookie2 = new Cookie("loginpwd", password);
            Cookie mycookie4 = new Cookie("remember2", "checked");
            response.addCookie(mycookie2);
            response.addCookie(mycookie4);
        }
        out.println("<a href=index.jsp>重新登录</a>");
    } else {
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }
%>
</body>
</html>

上一篇:第一章 JAVA语言概述与开发


下一篇:j2ee实现猜数游戏(网页版)