会话技术-Cookid-发送多个Cookie
1. 一次可不可以发送多个cookie?
可以
可以创建多个Cookie对象,使用response调用多次addCookie方法发送cookie即可。
package com.example.day_16_cookidsession.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快速入门 * */ @WebServlet("/cookieDemo3") public class CookieDemo3 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); } }
会话技术-Cookid-Cookie存活时间
2. cookie在浏览器中保存多长时间?
1. 默认情况下,当浏览器关闭后,Cookie数据被销毁
2. 持久化存储:
setMaxAge(int seconds)
1. 正数:将Cookie数据写到硬盘的文件中。持久化存储。并指定cookie存活时间,时间到后,cookie文件自动失效
2. 负数:默认值
3. 零:删除cookie信息
package com.example.day_16_cookidsession.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快速入门 * */ @WebServlet("/cookieDemo4") public class CookieDemo4 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); } }
会话技术-Cookid-Cookie存储中文
3. cookie能不能存中文?
在tomcat 8 之前 cookie中不能直接存储中文数据。
需要将中文数据转码---一般采用URL编码(%E3)
在tomcat 8 之后,cookie支持中文数据。特殊字符还是不支持,建议使用URL编码存储,URL解码解析
会话技术-Cookid-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.day_16_cookidsession.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快速入门 * */ @WebServlet("/cookieDemo5") public class CookieDemo5 extends HttpServlet { 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); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
标签:response,会话,Cookie,Cookid,import,cookie,servlet,javax From: https://www.cnblogs.com/yuzong/p/16942528.html