IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> JavaWeb中的会话跟踪技术Cookie和Seesion区别介绍详解 -> 正文阅读

[系统运维]JavaWeb中的会话跟踪技术Cookie和Seesion区别介绍详解


一、会话跟踪技术


1、会话跟踪技术介绍

        * 会话: 用户打开浏览器,访问web服务器的资源,会话建立,直到有一方断开连接,会话结束。
                  在一次会话中可以包含"多次请求和响应"   

2、为什么需要会话跟踪技术?

             *  因为HTTP协议是无状态的,每次浏览器向服务器请求时,服务器都会将该请求视为新的请求,
                 
                因此我们需要"会话跟踪技术来实现会话内数据共享" 
                 
                 
             *  实现方式:
           
                   "客户端会话跟踪技术": "Cookie"
                 
                   "服务端会话跟踪技术": "Session" 

二、 Cookie和Seesion详解


1、Cookie


(1)Cookie是什么 ?

           Cookie: "客户端会话技术",将数据保存到客户端,以后"每次请求都携带Cookie数据进行访问"

(2)为什么要使用Cookie?解决了什么问题 ?

             * "web程序是使用HTTP协议传输的",而HTTP协议是"无状态的协议""对于事务处理没有记忆能力"
             
                "缺少状态意味着如果后续处理需要前面的信息,则它必须重传",这样可能导致每次连接传送的数据量增大
               
                另一方面,如果服务器不需要获取之前的信息的时候它的应答就较快

             * "使用cookie可以解决这个问题,实现数据的共享"

(3)Cookie什么时候产生 ?

          * 客户端向服务器端发送一个请求时,服务端向客户端发送一个Cookie然后浏览器将Cookie保存
          
          * Cookie有两种保存方式:
          
                       1.Cookie保存在内存中
                       
                       2.保存在客户端的硬盘中, 之后每次HTTP请求浏览器都会将Cookie发送给服务器端

在这里插入图片描述


(4)Cookie的工作原理

                 1. 浏览器端第一次发送请求到服务器端
                 
                 2. "服务器端创建Cookie",该Cookie中包含用户的信息,然后将该Cookie发送到浏览器端
                 
                 3. 浏览器端"再次访问服务器端"时会"携带服务器端创建的Cookie"
                 
                 4. 服务器端"通过Cookie中携带的数据区分不同的用户"

(5)Cookie的基本使用

             * 发送 Cookie
            
                   1. 创建Cookie对象,设置数据
            
                        Cookie cookie = new Cookie("key","value");   //键值



                   2. 发送Cookie到客户端:使用response对象
                       
                        response.addCookie(cookie);

------------------------------------------------------------------------------------------------------------

             * 获取 Cookie

                   3. 获取客户端携带的所有Cookie,使用request对象
                   
                       Cookie[] cookies = request.getCookies();


                   4. 遍历数组,获取每一个Cookie对象: for
                       
                       
                   5. 使用Cookie对象方法获取数据
                        
                         cookie.getName();     //获取键

                         cookie.getValue();    //获取值
                   

?代码演示

// 创建Cookie保存到浏览器

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;


@WebServlet("/a")
public class AServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.使用构造器创建Cookie对象
        Cookie cookie = new Cookie("username", "zhangsan");


        // 2.将Cookie写到浏览器端
        response.addCookie(cookie);


    }
}

------------------------------------------------------------------------------------------------------------

// 读取Cookie数据
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;


@WebServlet("/b")
public class BServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        // 1.调用request.getCookies()返回一个Cookie对象数组
        Cookie[] cookies = request.getCookies();


        // 2.如果不为空,则遍历每个Cookie对象
        if(cookies !=null){
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                String value = cookie.getValue();
                System.out.println(name+":"+value);
            }
        }else {
            // 3.如果数组为null,打印没有读取到Cookie
            System.out.println("浏览器没有cookie");
        }

    }
}

在这里插入图片描述


(6)Cookie 存活时间

   * 默认情况下,Cookie 存储在浏览器内存中,当浏览器关闭,内存释放,则Cookie被销毁
            
              * setMaxAge(int seconds): 设置Cookie存活时间, 秒为单位
              
                   正数:Cookie写入浏览器所在电脑的硬盘,"持久化存储" , 到时间自动删除
              
                   负数:  "默认值" , Cookie在当前浏览器内存中,当"浏览器关闭,则 Cookie被销毁":   立马删除对应 Cookie

?代码演示

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

// 创建Cookie保存到浏览器
@WebServlet("/a")
public class AServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.使用构造器创建Cookie对象

        Cookie cookie = new Cookie("username", "zhangsan");


        // 3.设置Cookie的过期时间,单位是秒
        cookie.setMaxAge(60*60*24*3);     //当前时间的3天后

        // 2.将Cookie写到浏览器端
        response.addCookie(cookie);

    }
}


在这里插入图片描述


(7)Cookie 存储中文

             * Tomcat7 Cookie 不能直接存储中文
                 
             * Tomcat8 Cookie 可以存储中文但不能存储空格
             
             * 如需要存储,则需要进行转码: URL编码
                 
                 * " 转码: URLEncoder.encode("需要转码的内容" , "编码格式") 
                 
                 * " 解码: URLDecoder.decode("解码的内容" , "用什么码来解")

?转码解码代码演示

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

// 创建Cookie保存到浏览器
@WebServlet("/a")
public class AServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //使用构造器创建Cookie对象 Tomcat 8Cookie 可以存储中文但不能存储空格
        //URL编码
                                              //第一个为内容   第二个为编码
        String username = URLEncoder.encode(" 张 三", "utf-8");
        

        Cookie cookie = new Cookie("username", username);

        cookie.setMaxAge(60*60);

        response.addCookie(cookie);
    }
}


------------------------------------------------------------------------------------------------------------


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;

// 读取Cookie数据
@WebServlet("/b")
public class BServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        // 1.调用request.getCookies()返回一个Cookie对象数组
        Cookie[] cookies = request.getCookies();

        // 2.如果不为空,则遍历每个Cookie对象
        if(cookies !=null){
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                String value = cookie.getValue();
                if(name.equals("username")){

                    //URL解码  第一个参数为解码的内容, 第二个参数为用什么码来解
                    value=URLDecoder.decode(value,"utf-8");
                }
                System.out.println(name+","+value);
            }
        }else {
        
            // 3.如果数组为null,打印没有读取到Cookie
            System.out.println("浏览器没有cookie");
        }

    }
}

在这里插入图片描述


在这里插入图片描述


?未转码代码演示

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

// 创建Cookie保存到浏览器
@WebServlet("/a")
public class AServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        Cookie cookie = new Cookie("username", "张 三");

        cookie.setMaxAge(60*60);

        response.addCookie(cookie);       
    }
}


在这里插入图片描述


(8)Cookie的缺点

            * "数量受到限制":
                     单个cookie保存的数据<=4KB,一个站点一般保存20~50Cookie
                       (不同浏览器不一样,SarafiChrome对每个域的Cookie数目没有严格限制)
                                                          
            * "是不安全的":
                     cookie对客户端是可见的,别有用心的人可以分析存放在本地的cookie并进行cookie欺骗,
                     所以它是不安全的
                                                           
            * "浏览器可以禁用Cookie",禁用Cookie后,也就无法享有Cookie带来的方便

(9)Cookie的使用场景

                              1.对安全性要求不高
                              
                              2.不需要大量数据

2、Seesion


(1)Session是什么 ?

          * Session: "服务端会话跟踪技术,将数据保存到服务端"
                        JavaEE 提供 "HttpSession接口", 来实现"一次会话的多次请求间数据共享功能"

在这里插入图片描述


(2)Session的基本使用

?

               * request.getSession();    //获取Session对象
                     
                     * Session对象功能
                         
                       * void setAttribute(String name, Object o): "存储数据到 session 域中"
                         
                       * Object getAttribute(String name) : 根据 key,"获取值"
                         
                       * void removeAttribute(String name): 根据 key,"删除该键值对"

?代码演示

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

// 获取Session对象存储数据
@WebServlet("/demo01")
public class SessionDemo01 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.获取Session对象
        HttpSession session = request.getSession();

        // 2.存储数据
        session.setAttribute("username","zhangsan");
        session.setAttribute("money",1000);
    }
}


------------------------------------------------------------------------------------------------------------


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

// 获取Session对象获取数据
@WebServlet("/demo02")
public class SessionDemo02 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.获取Session对象

        HttpSession session = request.getSession();


        // 2.获取数据并转为对应的类型
        String username = (String) session.getAttribute("username");
        Integer money = (Integer) session.getAttribute("money");


        System.out.println("username = " + username);
        System.out.println("money = " + money);
    }

}




在这里插入图片描述


(3)Session的工作原理

           1. 浏览器端第一次发送请求到服务器端,服务器端创建一个Session
              同时会创建一个特殊的Cookie(name为JSESSIONID的固定值,value为session对象的ID)
              然后将该Cookie发送至浏览器端
              
           2.浏览器端发送第N次请求到服务器端,浏览器端访问服务器端时就会携带该name为JSESSIONID的Cookie对象
           
           3.服务器端根据name为JSESSIONID的Cookievalue(sessionId),去查询Session对象,从而区分不同用户


在这里插入图片描述


(4)Session 的钝化、活化

 * 服务器重启后,Session中的数据是否还在?
     
              * 钝化:"服务器正常关闭后" , "Tomcat自动将 Session数据写入硬盘的文件中"
                
              * 活化: "再次启动服务器后","文件中加载数据到Session中"

步骤演示

在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


(5)Seesion的销毁和存活

            * "默认情况下" , 无操作 ,  "30分钟自动销毁"

            * 怎么可以立即销毁 ?
                
                     * invalidate();     //立即销毁后,后面的Seesion操作都无效了

            * 怎么查看Seesion的存活时间?
                  
                     * getMaxInactiveInterval();  
            
            * 怎么设置Seesion的存活时间?
                 
                     * 在WEB-INF中的web.xml中设置
                     
                          <session-config>
                                <session-timeout>30</session-timeout> //这里的单位是分钟
                          </session-config>

?查看存活时间代码演示

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

// 获取Session对象获取数据
@WebServlet("/demo02")
public class SessionDemo02 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        // 1.获取Session对象
        HttpSession session = request.getSession();


        // 3.获取Session过期时间: 默认1800秒 = 30分钟

        int max = session.getMaxInactiveInterval();      //获取Session自动销毁的时间
        System.out.println(max);


        // 2.获取数据并转为对应的类型
        String username = (String) session.getAttribute("username");
        Integer money = (Integer) session.getAttribute("money");


        System.out.println("username = " + username);
        System.out.println("money = " + money);
        
        
        //立即销毁
        session.invalidate();
        
    }

}



在这里插入图片描述


?设置存活时间代码演示

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">


<!--设置Seesion 销毁时间,这里的单位是分钟-->
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

</web-app>

在这里插入图片描述


3、Cookie和Session的区别(面试题)

                 1.存储位置不同  
                 
                        * "Cookie"的数据信息存放在"客户端浏览器"* "Session"的数据信息存放在"服务器"2.存储容量不同
                     
                        * "单个Cookie保存的数据<=4KB,一个站点一般保存20~50个Cookie"
                            (不同浏览器不一样,SarafiChrome对每个域的Cookie数目没有严格限制)

                     
                        * 对于Session来说并"没有上限",但出于对服务器端的性能考虑,
                          Session内不要存放过多的东西,并且设置Session删除机制 (或者采用缓存技术代替session)
                     
                 3.存储方式不同
                     
                        * Cookie"只能保管ASCII字符串",并需要通过编码方式存储为Unicode字符或者二进制数据
                       
                        * Session中能够"存储任何类型的数据" ,包括且不限于string,integer,list,map等
                     
                     
                 4.安全性不同
                     
                        * "Cookie对客户端是可见的",别有用心的人可以分析存放在本地的cookie并进行cookie欺骗,
                          所以它是"不安全"* "Session存储在服务器上,不存在敏感信息泄漏的风险"
                     
                 5.存储时间不同
                      
                        *  Cookie默认浏览器关闭

                     
                        *  Session 默认30分钟
                     
                     
                        * 开发可以通过设置cookie的属性,达到使cookie长期有效的效果

                        * session依赖于名为JSESSIONID的cookie,而cookie JSESSIONID的过期时间默认为-1,
                          只需关闭窗口该session就会失效,因而session不能达到长期有效的效果
 
                     
                  6.服务器压力不同
                     
                       * Cookie保管在客户端,"不占用服务器资源", "对于并发用户十分多的网站,cookie是很好的选择"
                                       (Cookie不占用服务器资源)
                     
                       * session是保管在服务器端的,每个用户都会产生一个session, 假如并发访问的用户十分多,会产生十分
                         多的session,耗费大量的内存
                                       (Session 占用服务器资源)
                     
                   7.键值对数量不同
                     
                       * Cookie 存一个键和一个值
                     
                       * Session 存n个键和值
/*

Cookie 和 Session 都是来完成一次会话内多次请求间数据共享的

*/

作者:KJ.JK

文章对你有所帮助的话,欢迎给个赞或者 star,你的支持是对作者最大的鼓励,不足之处可以在评论区多多指正,交流学习

  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2022-06-14 22:57:06  更:2022-06-14 22:59:32 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/18 23:55:02-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码