Cookie的细节
1.一次可不可以发送多个cookie?
《上一博客》
2. cookie在浏览器中保存多长时间?
1. 默认情况下,当浏览器关闭后,Cookie数据被销毁
2. 持久化存储:
setMaxAge(int seconds)
1. 正数:将Cookie数据写到硬盘的文件中。持久化存储。并指定cookie存活时间,时间到后,cookie文件自动失效
2. 负数:默认值
3. 零:删除cookie信息
3. cookie能不能存中文?
在tomcat 8 之前 cookie中不能直接存储中文数据。
在tomcat 8 之后,cookie支持中文数据。特殊字符还是不支持,建议使用URL编码存储,URL解码解析
在tomcat 8 之后,cookie支持中文数据。特殊字符还是不支持,建议使用URL编码存储,URL解码解析
4. cookie获取范围多大
@WebServlet("/cookieDemo3") public class CookieDemo03 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.创建Cookie对象 Cookie c1 = new Cookie("msg","hello"); Cookie c2 = new Cookie("name","zhangsan"); //2.发送Cookie response.addCookie(c1); response.addCookie(c2); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
@WebServlet("/cookieDemo4") public class CookieDemo04 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.创建Cookie对象 Cookie c1 = new Cookie("msg","setMaxAge"); //2.设置cookie的存活时间 //c1.setMaxAge(30);//将cookie持久化到硬盘,30秒后会自动删除cookie文件 //c1.setMaxAge(-1); //c1.setMaxAge(300); c1.setMaxAge(0);//删除Cookie //3.发送Cookie response.addCookie(c1); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
标签:cookie,742,743,request,setMaxAge,Cookie,Cooke,c1,response From: https://www.cnblogs.com/agzq/p/17110192.html