Cookie和Session区别文档

cookie是客户端的,session是服务端的。 cookie存储于客户端,记录web服务器的信息,每次上网时都会先查看对应的cookie信息,比如购物时,使用cookie记录购物车信息。 session是记录客户机的信息,SessionID是session的唯一标识,使用session可以记录客户端的请求等。
https://blog.csdn.net/qq_42651904/article/details/85543640

Session的优点及其发送流程

Session是基于Cookie的

因为很多第三方可以获取到这个Cookie,服务器无法判断Cookie是不是真实用户发送的,所以Cookie可以伪造,伪造Cookie实现登录进行一些HTTP请求。如果从安全性上来讲,Session比Cookie安全性稍微高一些,我们先要知道一个概念—SessionID。SessionID是什么?客户端第一次请求服务器的时候,服务器会为客户端创建一个Session,并将通过特殊算法算出一个session的ID,下次请求资源时(Session未过期),浏览器会将sessionID(实质是Cookie)放置到请求头中,服务器接收到请求后就得到该请求的SessionID,服务器找到该id的session返还给请求者使用。

Session 基本使用

image.png

案例练习

创建servlet

  1. package com.taotao.web.session;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.HttpSession;
  8. import java.io.IOException;
  9. /**
  10. * create by 刘鸿涛
  11. * 2022/4/2 15:06
  12. */
  13. @SuppressWarnings({"all"})
  14. @WebServlet("/demo1")
  15. public class SessionDemo1 extends HttpServlet {
  16. @Override
  17. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  18. //存储到Session中
  19. //1.获取Session对象
  20. HttpSession session = req.getSession();
  21. //2.存储数据
  22. session.setAttribute("username","liuhongtao");
  23. }
  24. @Override
  25. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  26. this.doGet(req, resp);
  27. }
  28. }
  1. package com.taotao.web.session;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.HttpSession;
  8. import java.io.IOException;
  9. /**
  10. * create by 刘鸿涛
  11. * 2022/4/2 15:06
  12. */
  13. @SuppressWarnings({"all"})
  14. @WebServlet("/demo2")
  15. public class SessionDemo2 extends HttpServlet {
  16. @Override
  17. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  18. //获取数据,从session中
  19. //1.获取session对象
  20. HttpSession session = req.getSession();
  21. //2.获取数据
  22. Object username = session.getAttribute("username");
  23. System.out.println(username);
  24. }
  25. @Override
  26. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  27. this.doGet(req, resp);
  28. }
  29. }

测试运行

:::info 先访问demo1,再访问demo2 ::: image.png

Session原理image.png

案例演示

更改servlet,输出session地址值

  1. package com.taotao.web.session;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.HttpSession;
  8. import java.io.IOException;
  9. /**
  10. * create by 刘鸿涛
  11. * 2022/4/2 15:06
  12. */
  13. @SuppressWarnings({"all"})
  14. @WebServlet("/demo1")
  15. public class SessionDemo1 extends HttpServlet {
  16. @Override
  17. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  18. //存储到Session中
  19. //1.获取Session对象
  20. HttpSession session = req.getSession();
  21. System.out.println(session);
  22. //2.存储数据
  23. session.setAttribute("username","liuhongtao");
  24. }
  25. @Override
  26. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  27. this.doGet(req, resp);
  28. }
  29. }
  1. package com.taotao.web.session;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.HttpSession;
  8. import java.io.IOException;
  9. /**
  10. * create by 刘鸿涛
  11. * 2022/4/2 15:06
  12. */
  13. @SuppressWarnings({"all"})
  14. @WebServlet("/demo2")
  15. public class SessionDemo2 extends HttpServlet {
  16. @Override
  17. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  18. //获取数据,从session中
  19. //1.获取session对象
  20. HttpSession session = req.getSession();
  21. System.out.println(session);
  22. //2.获取数据
  23. Object username = session.getAttribute("username");
  24. System.out.println(username);
  25. }
  26. @Override
  27. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  28. this.doGet(req, resp);
  29. }
  30. }

测试运行

:::info 先访问demo1,再访问demo2 ::: image.png :::info 结论:session地址值完全一样 :::

主要JESSIONIDimage.png

Session使用细节

Session钝化、活化image.png

安全运行

image.png

访问demo1后关闭服务器

安全停止

:::info ctrl + c ::: image.png

再次安全运行

image.png

访问demo2

image.png

Session销毁:

image.png

演示更改Session销毁时间

更改web.xml文件

  1. <!DOCTYPE web-app PUBLIC
  2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
  4. <web-app>
  5. <session-config>
  6. <session-timeout>
  7. <!-- 默认30分钟,这里配置了100分钟后销毁-->
  8. 100
  9. </session-timeout>
  10. </session-config>
  11. </web-app>

演示直接销毁Session

更改sevletdemo2

  1. package com.taotao.web.session;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.HttpSession;
  8. import java.io.IOException;
  9. /**
  10. * create by 刘鸿涛
  11. * 2022/4/2 15:06
  12. */
  13. @SuppressWarnings({"all"})
  14. @WebServlet("/demo2")
  15. public class SessionDemo2 extends HttpServlet {
  16. @Override
  17. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  18. //获取数据,从session中
  19. //1.获取session对象
  20. HttpSession session = req.getSession();
  21. System.out.println(session);
  22. //销毁
  23. session.invalidate();
  24. //2.获取数据
  25. Object username = session.getAttribute("username");
  26. System.out.println(username);
  27. }
  28. @Override
  29. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  30. this.doGet(req, resp);
  31. }
  32. }

测试运行

:::info 访问demo1,再访问demo2 ::: image.png

会话跟踪技术小结

cookie和session的区别

image.png