• 会话跟踪技术(Cookie和Session)


    概述

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

    HTTP协议是无状态的,每次同一浏览器向服务器请求时,服务器都会将该请求视为新的请求,因此我们需要会话跟踪技术来实现同一会话内数据共享

    实现方式

    • 客户端会话跟踪技术:Cookie
    • 服务端会话跟踪技术:Session

    Cookie基本使用

    Cookie原理

    Cookie的实现是基于HTTP协议的

    • 响应头:set-cookie
    • 请求头:cookie

    Cookie 存活时间

    • 默认情况下,Cookie 存储在浏览器内存中,当浏览器关闭,内存释放,则Cookie被销毁
    • setMaxAge(int seconds):设置Cookie存活时间,但是是秒
      • 正数:将 Cookie写入浏览器所在电脑的硬盘,持久化存储。到时间自动删除
      • 负数:默认值,Cookie在当前浏览器内存中,当浏览器关闭,则 Cookie被销毁
      • 零:立即过期
    @WebServlet(value = "/a")
    public class AServlet extends HttpServlet {
        /*
        cookie细节:
        1.tomcat在8之后支持cookie的中文,但是不支持空格
        我们通过URL编码方式来传入空格
        2.设置cookie的过期时间
        setMaxAge(int i)
        如果传入正数就是多少秒后失效,保存在硬盘
        传入负数(默认)浏览器关闭后失效,保存在内存
        传入0 :立即失效
         */
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //get请求
            //传入空格 报错
            String name = "zhang san";//java.lang.IllegalArgumentException: Cookie值中存在无效字符
            //解决,URL编码
            name = URLEncoder.encode(name,"utf-8");
    
            //新建一个cookie
            //Cookie cookie = new Cookie("name","zhangsan");
            Cookie cookie = new Cookie("name",name);
    
            //cookie存活时间(一般看到用long修饰就是毫秒,用int就是秒)在这个cookie单位是秒
            cookie.setMaxAge(60*60);
    
            //发给客户端
            response.addCookie(cookie);
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //post请求
            doGet(request, response);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    @WebServlet(value = "/b")
    public class BServlet extends HttpServlet {
        /*
        浏览器发送一个请求到服务端,服务端创建一个cookie,用response将cookie放在响应头的set-cookie中
        浏览器看到set-cookie,将cookie解析出来,保存在浏览器
        下一次访问将cookie放在请求头中的cookie中
        实现数据的共享。
         */
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //get请求
            //获取cookie
            Cookie[] cookies = request.getCookies();
            if(cookies!=null){
                for (Cookie cookie : cookies) {
    
                    String name = cookie.getName();
                    String value = cookie.getValue();
                    value=URLDecoder.decode(value,"utf-8");
                    System.out.println(name+"::"+value);
                }
            }else{
                System.out.println("cookie不存在");
            }
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //post请求
            doGet(request, response);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    Session基本使用

    服务端会话跟踪技术:将数据保存到服务端

    JavaEE 提供 HttpSession接口,来实现一次会话的多次请求间数据共享功能

    Session原理

    Session是基于Cookie实现的

    Session使用细节

    Session 钝化、活化:

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

    本身tomcat是不用配置钝化和活化,但是idea集成的idea需要手动开启

    在这里插入图片描述

    Seesion 销毁:

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

    可以通过web.xml进行配置,单位为分钟

    调用 Session对象的 invalidate()方法

    @WebServlet(value = "/c")
    public class CServlet extends HttpServlet {
        /*
        session的钝化、活化
        钝化:session是保存在服务端的,在服务端正常关闭后,session持久化到硬盘中
        活化:在服务器启动后,将session文件重新加载到服务器中
         */
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //get请求
            HttpSession session = request.getSession();
            boolean aNew = session.isNew();
            String id = session.getId();
            System.out.println("是否新创建:"+aNew+" id:"+id);
    
            //获取存活时间 存活时间是最后一次操作后,默认再加30分钟,可以在web.xml中配置session销毁时间
            int maxInactiveInterval = session.getMaxInactiveInterval();
            System.out.println(maxInactiveInterval);
    
            //销毁 可以用在系统的退出功能中
            //session.invalidate();
    
            session.setAttribute("age",18);
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //post请求
            doGet(request, response);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    @WebServlet(value = "/d")
    public class DServlet extends HttpServlet {
        /*
        session是基于cookie实现的,request.getSession()时,在服务端创建一个session
        这个session有一个JSESSIONID,服务器自动将JSESSIONID放在响应头中的Set-Cookie中
        浏览器一看到Set-Cookie就将JSESSIONID解析出来保存到浏览器
        下一次浏览器访问其他资源时,将JSESSIONID放在请求头的cookies中
        服务器收到cookie中的JSESSIONID,通过这个id获取session对象
        实现了数据的共享(session保存在服务端)
         */
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //get请求
            //获取session
            HttpSession session = request.getSession();
            //判断session是不是新建的
            boolean aNew = session.isNew();
            //获取JSESSIONID
            String id = session.getId();
    
            System.out.println("是否新创建:"+aNew+"  id:"+id);
            Object age = session.getAttribute("age");
            System.out.println(age);
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //post请求
            doGet(request, response);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    在这里插入图片描述

    Cookie和Session的对比

    • 相同点
      • Cookie 和 Session 都是来完成一次会话内多次请求间数据共享的
    • 区别
      • 键值对数量:Cookie 存一个键和一个值,Session 存n个键和值
      • 存储位置:Cookie 是将数据存储在客户端,Session 将数据存储在服务端
      • 安全性:Cookie 不安全,Session 安全
      • 数据大小:Cookie 最大4KB,Session 无大小限制
      • 存储时间:Cookie默认浏览器关闭,Session 默认30分钟
      • 服务器性能:Cookie 不占服务器资源,Session 占用服务器资源

    最后

    如果你对本文有疑问,你可以在文章下方对我留言,敬请指正,对于每个留言我都会认真查看。

  • 相关阅读:
    SpringBoot WEB开发
    Python在WRF模型自动化运行及前后处理中的应用
    Requests 与接口请求构造
    2022 AI趋势8大预测
    激光切割机机械结构设计
    【2023云栖】刘一鸣:Data+AI时代大数据平台建设的思考与发布
    关于对Java泛型的解释和思考
    使用Mock技术模拟数据
    swiper 左右保留部分
    javaweb没有web.xml设置方法
  • 原文地址:https://blog.csdn.net/weixin_47543906/article/details/128209024