会话技术-Cookie-发送多个Cookie
1.一次可不可以发送多个Cookie?
可以
可以创建多个Cookie对象,使用response调用多次addCookie方法发送cookie即可。
package com.example.cookie; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * cookie细节1:发送多个Cookie */ @WebServlet("/cookieDemo3") public class cookieDemo3 extends HttpServlet { @Override 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); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
会话技术-Cookie-Cookie存活时间
2.cookie在浏览器中保存多长时间?
1.默认情况下,当浏览器关闭后,Cookie数据被销毁
2.持久化存储:
setMaxAge(int seconds)
1.正数:将Cookie数据写到硬盘的文件中。持久化存储。并指定cookie存活时间,时间到后,cookie文件自动失效
2.负数:默认值
3.零:删除cookie信息
package com.example.cookie; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * cookie细节2:Cookie存活时间 */ @WebServlet("/cookieDemo4") public class cookieDemo4 extends HttpServlet { @Override 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); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
会话技术-Cookie-Cookie存储中文
3.cookie能不能存中文?
在tomcat 8 之前 cookie中不能直接存储中文数据。
需要将中文数据转码--一般采用URL编码(%E3)
在tomcat 8 之后,cookie支持中文数据。特殊字符还是不支持,建议使用URL编码存储,URL解码分析
会话技术-Cookie-Cookie共享
1.假设在一个tomcat服务器中,部署了多个web项目,那么在这些web项目中cookie能不能共享?
默认情况下cookie不能共享
setPath(String path):设置cookie的获取范围。默认情况下,设置当前的虚拟目录
如果要共享,则可以将path设置为"/"
2.不同的tomcat服务器简cookie共享问题?
setDomain(String path):如果设置一级域名相同,那么多个服务器之间的cookie可以共享
setDomain(".baidu.com"),那么tieba.baidu.com和news.baidu.com中的cookie可以共享
package com.example.cookie; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * cookie细节4:Cookie共享 */ @WebServlet("/cookieDemo5") public class cookieDemo5 extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 1.创建Cookie对象 Cookie c1 = new Cookie("msg", "你好"); // 设置path,让当前服务器下部署的手游项目共享Cookie信息 c1.setPath("/"); // 3.发送Cookie response.addCookie(c1); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }标签:cookie,存活,response,会话,Cookie,import,servlet,javax From: https://www.cnblogs.com/wsfj/p/17108242.html