• Cookie和session的区别


    文章目录



    前言

    一、Cookie

    1.定义

    在这里插入图片描述

    2.Cookie的存活时间

    在这里插入图片描述

    3.Cookie存储中文

    在这里插入图片描述

    4.Cookie基本用法

    在这里插入图片描述

    package com.jkj.web.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("/first")
    public class FirstServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //创建cookie
            Cookie cookie = new Cookie("userName","jack");
            String value="小马哥";
            //url编码
            URLEncoder.encode(value,"utf-8");
            Cookie cookie1 = new Cookie("userName",value);
            //设置存活时间 60秒
            //cookie.setMaxAge(60);
            //发送cookie,response
            resp.addCookie(cookie);
            resp.addCookie(cookie1);
        }
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            this.doGet(req, resp);
        }
    }
    
    
    package com.jkj.web.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("/second")
    public class SecondServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //获取cookie
            //获取cookie数组
            Cookie[] cookies = req.getCookies();
            //遍历数组
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                if ("userName".equals(name)){
                    String value = cookie.getValue();
                    //解码
                    String values = URLDecoder.decode(value, "utf-8");
                    //获取数据
                    System.out.println(name+":"+values);
                    //结束循环
                    break;
                }
            }
    
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            this.doGet(req, resp);
        }
    }
    
    
    • 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
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74

    二、Session

    1.Session活化

    在这里插入图片描述

    2.Session的使用

    在这里插入图片描述

    3.Session的销毁

    在这里插入图片描述

    4.Session实例

    package com.jkj.web.session;
    
    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;
    @WebServlet("/test1")
    public class SessionTest1 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //获取session对象
            HttpSession session = req.getSession();
            //存储数据
            session.setAttribute("key1","jack");
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            this.doGet(req, resp);
        }
    }
    
    
    package com.jkj.web.session;
    
    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;
    @WebServlet("/test2")
    public class SessionTest2 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //获取session对象
            HttpSession session = req.getSession();
            //接收数据
            Object value = session.getAttribute("key1");
            System.out.println(value);  //jack
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            this.doGet(req, resp);
        }
    }
    
    
    • 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
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    三,Cookie和session的区别

    在这里插入图片描述

  • 相关阅读:
    【JavaSE】抽象类与接口
    创建文件夹
    k8s快速入门教程-----7 数据管理
    【寒武纪(13)】BANGC 报错 stack smashing detected
    跨境电商短视频营销:这7点必须做好
    基于 QT 实现一个 Ikun 专属桌面宠物
    mklink命令使得OneDrive同步任意一个文件夹
    python中多行注释与取消注释
    当有人知道你的愿望想帮你实现你会是怎样
    第6节-PhotoShop基础课程-认识选区
  • 原文地址:https://blog.csdn.net/qq_43514330/article/details/125335295