Cookie的细节:
- 一次发送多个cookie
- 可以创建多个Cookie对象,使用resopnse调用多次addCookie方法发送cookie即可
package com.ailyt.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(value = "/CookieStudy03")
public class CookieStudy03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie c1 = new Cookie("msg", "hello");
Cookie c2 = new Cookie("name", "zhangsan");
response.addCookie(c1);
response.addCookie(c2);
Cookie[] cs = request.getCookies();
for (Cookie cookie1 : cs) {
System.out.println(cookie1.getName() + "--->" + cookie1.getValue());
}
}
}
- cookie在浏览器中保存多长时间?
- 默认情况下,当浏览器关闭,Cookie数据销毁
- 持久化存储:
- setMaxAge(int seconds)
- 正数:将Cookie数据写到硬盘的文件中,持久化存储。Cookie存活时间
- 负数:默认值,当浏览器关闭后,Cookie数据被摧毁
- 零:删除cookie信息
package com.ailyt.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(value = "/CookieStudy04")
public class CookieStudy04 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//创建Cookie对象
Cookie c1 = new Cookie("msg", "setMaxAge");
//设置Cookie的存活时间
//c1.setMaxAge(30);//将Cookie持久化到磁盘,30秒之后自动删除
//c1.setMaxAge(-1);//默认值,当浏览器关闭后,Cookie数据被摧毁
//c1.setMaxAge(0);//删除cookie信息
//发送Cookie
response.addCookie(c1);
}
}
- cookie能不能存中文?
- 在tomcat 8 之前cookie不能直接存储中文数据
- 需要讲中文数据转码---一般采用URL编码(%E3)
- 在tomcat 8 之后,在cookie支持中文数据
package com.ailyt.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(value = "/CookieStudy05")
public class CookieStudy05 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie c1 = new Cookie("msg", "我是张三,你好");
response.addCookie(c1);
}
}
- cookie共享问题?
- 假设在一个tomcat服务器中,部署了多个web项目,那么在这些web项目中cookie能不能共享?
- 默认情况下cookie不可以共享
- setPath(String path):设置cookie的获取范围。默认情况下,设置当前的虚拟目录