• JavaWeb之Cookie的简单使用!!!


    什么是Cookie

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

    Cookie 数据存放在浏览器端(客户端)

    创建Cookie

    1.创建Cookie

    Cookie cookie = new Cookie("key","value");

    2.使用response响应Cookie给客户端(浏览器)

    response.addCookie(cookie);

    1. /*
    2. * Copyright (c) 2020, 2023, All rights reserved.
    3. *
    4. */
    5. package cn;
    6. import jakarta.servlet.ServletException;
    7. import jakarta.servlet.annotation.WebServlet;
    8. import jakarta.servlet.http.Cookie;
    9. import jakarta.servlet.http.HttpServlet;
    10. import jakarta.servlet.http.HttpServletRequest;
    11. import jakarta.servlet.http.HttpServletResponse;
    12. import java.io.IOException;
    13. import java.lang.annotation.Retention;
    14. /**
    15. *

      Project: scl-cookie - AddCookie

    16. *

      Powered by scl On 2023-09-04 16:36:59

    17. *

      描述:

    18. *
    19. * @author 孙臣龙 [1846080280@qq.com]
    20. * @version 1.0
    21. * @since 17
    22. */
    23. @WebServlet("/addCookie")
    24. public class AddCookie extends HttpServlet {
    25. @Override
    26. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    27. //创建Cookie
    28. Cookie cookie = new Cookie("meite","scl");
    29. //设置存活时间
    30. cookie.setMaxAge(60);
    31. //添加Cookie
    32. resp.addCookie(cookie);
    33. }
    34. }

    3.获取Cookie

    Cookie[] cookies = request.getCookies();
    for(Cookie str : cookies){
    System.out.println(str.getName()+str.getValue());
    }

    1. /*
    2. * Copyright (c) 2020, 2023, All rights reserved.
    3. *
    4. */
    5. package cn;
    6. import jakarta.servlet.ServletException;
    7. import jakarta.servlet.annotation.WebServlet;
    8. import jakarta.servlet.http.Cookie;
    9. import jakarta.servlet.http.HttpServlet;
    10. import jakarta.servlet.http.HttpServletRequest;
    11. import jakarta.servlet.http.HttpServletResponse;
    12. import java.io.IOException;
    13. /**
    14. *

      Project: scl-cookie - GetCookie

    15. *

      Powered by scl On 2023-09-04 16:43:04

    16. *

      描述:

    17. *
    18. * @author 孙臣龙 [1846080280@qq.com]
    19. * @version 1.0
    20. * @since 17
    21. */
    22. @WebServlet("/getCookie")
    23. public class GetCookie extends HttpServlet {
    24. @Override
    25. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    26. //获取Cookie
    27. Cookie[] cookies = req.getCookies();
    28. for(Cookie str : cookies){
    29. System.out.println(str.getName()+str.getValue());
    30. }
    31. }
    32. }

    Cookie过期时间

    setMaxAge(int seconds):设置Cookie存活时间

    1.正数:将Cookie写入浏览器所在的电脑硬盘,持久化存储,到期自动删除 

    2.负数:默认值,Cookie存储在浏览器内存中,当浏览器关闭,内存释放,则Cookie被销毁。

    3.零:删除对应Cookie

    1. //创建Cookie
    2. Cookie cookie = new Cookie("meite","scl");
    3. //设置存活时间
    4. cookie.setMaxAge(60);
    5. //添加Cookie
    6. resp.addCookie(cookie);

  • 相关阅读:
    哪些因素会导致咽炎?
    OPengl学习(二)——opengl环境搭建
    运用程序化交易系统的能力表现在哪些方面?
    【PWN · 栈迁移】[CISCN 2019东南]PWN2
    ubuntu下网卡插入网线后仍然不连接
    python多进程multiprocessing
    wordpress搭建自己的博客详细过程以及踩坑
    Debezium系列之:深入理解Kafka的消息代理
    dev board sig技术文章:轻量系统适配ARM架构芯片平台
    LVS+Keepalived+NFS集群部署
  • 原文地址:https://blog.csdn.net/qq_64847107/article/details/132673399