Cookie

HTTP Cookie(也叫 Web Cookie 或浏览器 Cookie)是服务器发送到用户浏览器并保存在本地的一小块数据,它会在浏览器下次向同一服务器再发起请求时被携带并发送到服务器上。通常,它用于告知服务端两个请求是否来自同一浏览器,如保持用户的登录状态。

会话跟踪技术

原理图image.png

Cookie

image.png

Cookie基本使用

创建web项目

  1. 创建webapp骨架项目
  2. 导入pom.xml坐标
  3. 创建java和resources目录

image.png

发送Cookie案例

编写Servlet

  1. package com.taotao.web;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.Cookie;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. /**
  10. * create by 刘鸿涛
  11. * 2022/4/1 13:29
  12. */
  13. @WebServlet("/aServlet")
  14. public class AServlet extends HttpServlet {
  15. @Override
  16. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  17. //发送Cookie
  18. //1.创建Cookie对象 键、对值
  19. Cookie cookie = new Cookie("username","taotao");
  20. //2.发送Cookie,response
  21. resp.addCookie(cookie);
  22. }
  23. @Override
  24. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  25. this.doGet(req, resp);
  26. }
  27. }

运行测试-发送Cookieimage.png

得到Cookie案例

编写Servlet

  1. package com.taotao.web;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.Cookie;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. /**
  10. * create by 刘鸿涛
  11. * 2022/4/1 13:29
  12. */
  13. @WebServlet("/bServlet")
  14. @SuppressWarnings({"all"})
  15. public class BServlet extends HttpServlet {
  16. @Override
  17. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  18. //获取Cookie
  19. //获取Cookie数组
  20. Cookie[] cookies = req.getCookies();
  21. //遍历数组
  22. for (Cookie cookie: cookies) {
  23. //获取数据
  24. String name = cookie.getName();
  25. if("username".equals(name)){
  26. String value = cookie.getValue();
  27. System.out.println(name + " :" + value);
  28. }
  29. break;
  30. }
  31. }
  32. @Override
  33. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  34. this.doGet(req, resp);
  35. }
  36. }

运行测试-获取Cookie

:::info 先访问aServlet发送cookie ::: image.png :::info 访问bServlet得到发送的cookie ::: image.png

Cookie原理

image.png

响应头set-cookie

image.png

Cookie使用细节

image.png

cookie存活时间

image.png

:::info 设置cookie存活时间,可以让cookie驻留在电脑硬盘上 :::

编写aServlet

  1. package com.taotao.web;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.Cookie;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. /**
  10. * create by 刘鸿涛
  11. * 2022/4/1 13:29
  12. */
  13. @WebServlet("/aServlet")
  14. public class AServlet extends HttpServlet {
  15. @Override
  16. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  17. //发送Cookie
  18. //1.创建Cookie对象 键、对值
  19. Cookie cookie = new Cookie("username","taotao");
  20. //设置cookie的存活时间 1周7天
  21. cookie.setMaxAge(60*60*24*7); //单位为秒s
  22. //2.发送Cookie,response
  23. resp.addCookie(cookie);
  24. }
  25. @Override
  26. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  27. this.doGet(req, resp);
  28. }
  29. }

运行测试

:::info 先访问aServlet,然后关闭浏览器 ::: image.png :::info 再次打开浏览器,访问bServlet,发现可以得到cookie :::

image.png

查看电脑硬盘存在的cookie

image.pngimage.png
image.pngimage.png

Cookie存储中文image.png

默认存储中文报错

image.png

进行转码:URL编码

  1. package com.taotao.web;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.Cookie;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. import java.net.URLEncoder;
  10. /**
  11. * create by 刘鸿涛
  12. * 2022/4/1 13:29
  13. */
  14. @WebServlet("/aServlet")
  15. public class AServlet extends HttpServlet {
  16. @Override
  17. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  18. //发送Cookie
  19. //1.创建Cookie对象 键、对值
  20. String value = "涛涛";
  21. //URL编码
  22. value = URLEncoder.encode(value, "UTF-8");
  23. System.out.println("存储数据:" + value);
  24. Cookie cookie = new Cookie("username",value);
  25. //设置cookie的存活时间 1周7天
  26. cookie.setMaxAge(60*60*24*7); //单位为秒s
  27. //2.发送Cookie,response
  28. resp.addCookie(cookie);
  29. }
  30. @Override
  31. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  32. this.doGet(req, resp);
  33. }
  34. }

测试运行

image.png

进行解码:UT8-8

  1. package com.taotao.web;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.Cookie;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. import java.net.URLDecoder;
  10. /**
  11. * create by 刘鸿涛
  12. * 2022/4/1 13:29
  13. */
  14. @WebServlet("/bServlet")
  15. @SuppressWarnings({"all"})
  16. public class BServlet extends HttpServlet {
  17. @Override
  18. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  19. //获取Cookie
  20. //获取Cookie数组
  21. Cookie[] cookies = req.getCookies();
  22. //遍历数组
  23. for (Cookie cookie: cookies) {
  24. //获取数据
  25. String name = cookie.getName();
  26. if(name.equals("username")){
  27. String value = cookie.getValue();
  28. //URL解码
  29. value = URLDecoder.decode(value, "UTF-8");
  30. System.out.println(name + " :" + value);
  31. break;
  32. }
  33. }
  34. }
  35. @Override
  36. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  37. this.doGet(req, resp);
  38. }
  39. }

测试运行

image.png

Cookie自我理解

cookie的实现是基于HTTP协议的
学了这个cookie,感觉可有意思,还可以设置生命周期
感觉这个cookie中插入点木马病毒,应该也是可以的,
也就是说,只要你们用完浏览器不清除某个站点请求的cookie,那么,那个站点就有可能攻击你