• 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>

     

  • 相关阅读:
    字符串Hash 学习笔记
    P4315 月下“毛景树” 题解
    page
    Equation
    Graph
    配置UOJ数据的正确姿势
    luogu2261余数求和题解--整除分块
    luogu2858奶牛零食题解--区间DP
    luogu1005矩阵取数游戏题解--区间DP
    luogu4677山区建小学题解--区间DP
  • 原文地址:https://www.cnblogs.com/1ming/p/9474409.html
Copyright © 2020-2023  润新知