jsp04状态管理

1.http 协议的无状态性

无状态是指,当浏览器发送请求给服务器的时候,服务器会响应。但当同一个浏览器再次发送请求时,服务器不会知道是刚才那个浏览器。

简单说,服务器【不会保存用户状态】,不会记得客户端是否访问过,所以这就是无状态协议

2.保存用户的两大机制

Session-保存在服务器端
Cookie-保存在客户端

3.Cookie 技术

3.1Cookie :中文名称为“小甜饼”,是web服务器保存在客户端的一系列文本信息。
3.2使用场景:
(1)、判断用户是否已登录,n天内保存登录状态。
(2)“购物车”处理。
(3)、保存浏览记录。

缺点:容易泄露用户信息。

3.3Cookie 的作用

 (1)对特定对象的追踪;

(2)保存用户网页浏览记录与习惯;

( 3)简化登录。

(4)安全风险:容易泄露用户信息

3.4Jsp 中创建与使用Cookie 

 1)创建Cookie对象
   Cookie newCookie = new Cookie(String key , Object value);

 2)写入Cookie对象
response.addCookie(newCookie);

 3)读取Cookie对象
 Cookie[] cookies = request.getCookies();

3.5Cookie 的常用方法

类型 方法名 方法解释
String getComment() 返回cookie中注释,如果没有注释的话将返回空值.
String getDomain() 返回cookie中Cookie适用的域名. 使用getDomain() 方法可以指示浏览器把Cookie返回给同 一域内的其他服务器,而通常Cookie只返回给与发送它的服务器名字完全相同的服务器。注意域名必须以点开始(例如.yesky.com)
int getMaxAge() 返回Cookie过期之前的最大时间,以秒计算。
String getName() 返回Cookie的名字。名字和值是我们始终关心的两个部分,笔者会在后面详细介绍 getName/setName。
String getPath() 返回Cookie适用的路径。如果不指定路径,Cookie将返回给当前页面所在目录及其子目录下 的所有页面。
boolean getSecure() 如果浏览器通过安全协议发送cookies将返回true值,如果浏览器使用标准协议则返回false值。
String getValue() 返回Cookie的值。笔者也将在后面详细介绍getValue/setValue。
int getVersion() 返回Cookie所遵从的协议版本。
void setComment(String purpose) 设置cookie中注释。
void setDomain(String pattern) 设置cookie中Cookie适用的域名
void setMaxAge(int expiry) 以秒计算,设置Cookie过期时间。
void setPath(String uri) 指定Cookie适用的路径。
void setSecure(boolean flag) 指出浏览器使用的安全协议,例如HTTPS或SSL。
void setValue(String newValue) cookie创建后设置一个新的值。
void setVersion(int v) 设置Cookie所遵从的协议版本。   

3.6 Cookie的代码实例:

(1)案例:十天免登陆

//login.jsp 登陆页面
<%@ 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>
    <%
        String username = "";

        String password = "";

        Cookie[] cks = request.getCookies();
        if (cks != null && cks.length > 0) {
            for (Cookie c : cks) {
                if (c.getName().equals("username")) {
                    username = c.getValue();
                }
                if (c.getName().equals("password")) {
                    password = c.getValue();
                }
            }

        }
    %>





    <h1>post</h1>
    <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" checked="checked"
                    name="isUseCookie" value="十天内记住我的登陆状态" />十天内记住我的登陆状态</td>
            </tr>
            <br>
            <tr>
                

                <td><input type="submit" value="提交" /></td>

            </tr>
        </table>
    </form>


    // dolphin.jsp 处理登陆页面
<%@page import="java.net.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<h1> doLogin </h1>
<%
request.setCharacterEncoding("UTF-8");
String username = "";

String  password = "";
//处理中文乱码 (加编码)
username =URLEncoder.encode(request.getParameter("username"),"UTF-8") ;
password=URLEncoder.encode(request.getParameter("password"),"UTF-8") ;

password = request.getParameter("password");
String [] isUseCookie = request.getParameterValues("isUseCookie"); 

if(isUseCookie !=null &&  isUseCookie.length>0){
    //说明勾选了
    Cookie ck_username= new Cookie("username",username);
    Cookie ck_password= new Cookie("password",password);
    //设置Cookie 的有效期,以秒为单位
    ck_username.setMaxAge(60*60*24*10);//10天=864000秒
    
    ck_password.setMaxAge(864000);
        //写入Cookie 对象
    response.addCookie(ck_username);
    response.addCookie(ck_password);
}else{
    
    //如果说没有勾选
    Cookie [] ck=request.getCookies();//读取Cookie 对象
     for(Cookie c : ck){
         if(c.getName().equals("username")
                 || c.getName().equals("password")){
             //因为不保存,设为马上过期
             c.setMaxAge(0);
             
             response.addCookie(c);//因为要覆盖之前的
         }
     }
    
}


%>

//index.jsp 查看Cookie 数据的页面
<%@page import="java.net.URLDecoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

    <%
    request.setCharacterEncoding("UTF-8");
        String username = "";

        String password = "";

        Cookie[] cks = request.getCookies();
        if (cks != null && cks.length > 0) {
            for (Cookie c : cks) {
                if (c.getName().equals("username")) {
                    //处理中文乱码(解码)
                    username = URLDecoder.decode(c.getValue(),"UTF-8");
                }
                if (c.getName().equals("password")) {
                    password =  URLDecoder.decode(c.getValue(),"UTF-8");
                }
            }

        }
    %>
    cookie中的用户名:<%=username %>
cookie中的密码:<%=password %>
View Code

 3.7 Session 与 Cookie  的对比

1.保存位置:session在服务器端内存,cookie在客户端文本

2.保存对象:session保存Object类(保存对象大小没有限制),cookie保存String类型(保存对象大小有限制)

3.生存权:session会话结束即销毁,cookie可以长期保存在客户端

4.重要性:session安全性更高,保存重要信息,cookie保存不重要的信息

原文地址:https://www.cnblogs.com/sbj-dawn/p/7105273.html