Cookie细节存活时间
Cookie在浏览器中保存多长时间
默认情况下,当浏览器关闭后,Cooklie数据被销毁
持久化存储:
setMaxAge(int seconds)
正数:将Cookie数据写到硬盘的文件中,持久化储存,cookie存活时间
负数:默认值
零:删除cookie信息
Cookie储存中文数据
Cookie能不能存中文
在tomcat 8 之前 cookie中不能直接存储中文数据
在tomcat 8 之后 cookie支持中午数据。
案例:
@WebServlet(value = "/ServletCookie")
public class ServletCookie extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
/**
* 使用步骤:
*
* 创建Cookie对象,绑定数据
* new Cookie(String name,String value)
* 发送Cookie对象
* response,addCookie(Cookie cookie)
* 获取Cookie ,拿到数据
* Cookie【】 request.getCookies()
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 创建Cookie对象
Cookie cookie = new Cookie("mse", "你好");
// 发送Cookie对象
response.addCookie(cookie);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取Cookie ,拿到数据
// * Cookie【】 request.getCookies()
Cookie[] cookies = request.getCookies();
if (cookies!=null){
for (Cookie cookie : cookies) {
String name = cookie.getName();
String value = cookie.getValue();
System.out.println(name+":"+value);
}
}
}
}
标签:中文,String,request,value,存活,Cookie,cookie,response From: https://www.cnblogs.com/ssr1/p/16591229.html