cookie创建,使用 . session与Cookie区别

复制代码
<%@page import="java.net.URLDecoder"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Coolie创建</title>
</head>
<body>

    <h1>用户登录</h1>

    <hr>
    <%
    request.setCharacterEncoding("utf-8");
        String userName = "";
        String password = "";
        
        Cookie[] cookies=request.getCookies();
        if(cookies!=null&&cookies.length>0){
            for(Cookie c:cookies){
                if(c.getName().equals("userName")){
                    userName=URLDecoder.decode(c.getValue(), "utf-8");
                }
                if(c.getName().equals("password")){
                    password=URLDecoder.decode(c.getValue(), "utf-8");
                }
            }
        }
    %>
    <form action="dologin.jsp" name="loginForm" 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">10天内记住用户名</td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="">登录</td>
            </tr>
        </table>

    </form>

</body>
</html>
复制代码
复制代码
<%@page import="java.net.URLEncoder"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登陆成功或失败</title>
</head>
<body>
    <h1>登陆成功</h1>

    <br>
    <br>
    <br>
    <br>
    <br>

    <%
        /* 首先判断用户是否选择了记住用户名 */
        String[] isCookie=request.getParameterValues("isUseCookie");
        if(isCookie!=null&&isCookie.length>0){
            
            
            request.setCharacterEncoding("utf-8");
            
            //把用户名和密码保存再cookie对象里
            String userName=URLEncoder.encode(request.getParameter("userName"),"utf-8");
            //使用URL解决无法在cookie中无法保存中文
            String password=URLEncoder.encode(request.getParameter("password"),"utf-8");
            
            
            Cookie cookieName=new Cookie("userName",userName);
            Cookie cookiePassword=new Cookie("password",password);
            cookieName.setMaxAge(864000);
            cookiePassword.setMaxAge(864000);//设置最大生存期限为10天
            response.addCookie(cookieName);
            response.addCookie(cookiePassword);
            
        }else{
            //把已经保存的Cookie内容失效
            Cookie[] cookies=request.getCookies();
            if(cookies!=null&&cookies.length>0){
                for(Cookie c:cookies){
                    if(c.getName().equals("userName")||(c.getName().equals("password"))){
                        c.setMaxAge(0);//设置cookie失效
                        response.addCookie(c);
                    }
                }
            }
        }
    %>
    <a href="user.jsp" target="_blank">查看用户信息</a>

</body>
</html>
复制代码
复制代码
<%@page import="java.net.URLDecoder"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>用户信息</title>
</head>
<body>
    <h1>用户信息</h1>
    <hr>
    <br>
    <%
        request.setCharacterEncoding("utf-8");
        
        String userName = "";
        String password = "";
        
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0)
        {
            for (Cookie c : cookies)
            {
                if (c.getName().equals("userName"))
                {
                    userName = URLDecoder.decode(c.getValue(), "utf-8");
                }
                if (c.getName().equals("password"))
                {
                    password = URLDecoder.decode(c.getValue(), "utf-8");
                }
            }
        }
    %>
    <br>
    <br>
    <br> 用户名:<%=userName%>
    <br> 密码:<%=password%>
    <br>

</body>
</html>
复制代码

 

 

posted @   百事没事  阅读(653)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示