zoukankan      html  css  js  c++  java
  • 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>

     

  • 相关阅读:
    C++的几个晦涩特性
    高效学习的工具Mnemosyne
    允许OSQA上传任意类型的文件
    新一代J2ME终端标准MSAfor CLDC——解读JSR248
    JAVA实现简单的HTTP服务器
    什么是Web 2.0
    Java 手机与空间制图服务(WMS)应用集成
    广西移动SP结算工作管理流程
    2007年Enterprise 2.0的10大趋势
    移动虚拟专用网的发展与未来应用
  • 原文地址:https://www.cnblogs.com/1ming/p/9474409.html
Copyright © 2011-2022 走看看