Cookie学习

先创建一下cookie项目

在这里插入图片描述
修改index.jsp,并且新建三个页面:do_login.jsp login.jsp success.jsp

login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ 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 type="text" id = "uname" name="username"/></td>
        </tr>
        <tr>
            <td align="center">&nbsp;</td>
            <td><input type="password" id="upwd" name="password"/></td>
        </tr>
        <tr align="center">
            <td colspan="2">
                <input id="saveuname" type="checkbox" name="saveuname" value="记住用户名"/>记住用户名
                <input id="savepwd" type="checkbox" name="savepwd" value="记住密码" onclick="stechekuname()"/>记住密码
            </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 savepwd = "";
    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("savauname")){
            saveuname = cookie.getValue();
        }
        if (cookie.getName().equals("savepwd")){
            savepwd = cookie.getValue();
        }
    }
%>
<script type="text/javascript">
    var saveuname = document.getElementById("saveuname");
    if (saveuname.checked){
        var txtUname = document.getElementById("uname");
        txtUname.value = "<%= new String(uname.getBytes("iso-8859-1"),"utf-8") %>";
    }
    var savepwd = document.getElementById("savepwd");
    if (savepwd.checked){

        var txtUpwd = document.getElementById("upwd");
        txtUpwd.value = "<%= new String(upwd.getBytes("iso-8859-1"),"utf-8") %>";
    }
</script>
</body>
<html>

do_login.jsp

<%
    //获取登录表单的数据
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    String[] choice = request.getParameterValues("save");
    //判断用户是否登录成功
    if (username.equals("串串") && password.equals("111111")) {
        //创建cookie对象,写入客户端
        Cookie uname = new Cookie("uname",username);
        response.addCookie(uname);
        Cookie upwd = new Cookie("upwd",password);
        response.addCookie(upwd);
        Cookie saveuname = new Cookie("saveuname","no");
        Cookie savepwd = new Cookie("savepwd","no");
        if (choice != null){
            if (choice.length==2){
                saveuname.setValue("yes");
                savepwd.setValue("yes");
            }else {
                saveuname.setValue("yes");
            }
        }
        //采用重定向跳转到登录成功页面
        response.sendRedirect("success.jsp");
    } else {
        //采用重定向跳转到登录页面
        response.sendRedirect("login.jsp");
    }
%>

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();
            break;
        }
    }
%>
<h3><%= new String(uname.getBytes("ISO-8859-1"),"utf-8") %>,登录成功!</h3>
</body>
</html>

启动服务器查看效果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值