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); } }