posts - 397,comments - 0,views - 25332

Cookie的细节

  一次可不可以发送多个?

  可以

  可以创建多个Cookie对象使用Response调用多次addCookie方法cookie即可

 

复制代码
@WebServlet("/CookieDemo3")
public class CookieDemo3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Cookie cookie1 = new Cookie("msg","hello");
        Cookie cookie2 = new Cookie("name","zhangsan");
        response.addCookie(cookie1);
        response.addCookie(cookie2);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
复制代码

 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

cookie在浏览器中保存多长时间。

 默认情况下,当浏览关闭后,Cook数据被销毁

 持久化存储

   setMaxAge(int seconds)

   正数:将Cookie数据写到硬盘的文件中。持久化存储。cookie存活时间

     负数:默认值

      零:删除cookie信息

复制代码
@WebServlet("/CookieDemo4")
public class CookieDemo4 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Cookie cookie1 = new Cookie("msg","setMaxAge");
//        cookie1.setMaxAge(30);
//        cookie1.setMaxAge(-1);
//        cookie1.setMaxAge(300);
        cookie1.setMaxAge(0);
        response.addCookie(cookie1);



    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
复制代码

 

posted on   淤泥不染  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示