会话概述![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/98403331ba634c62e5c75f154c0b7a42.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e7770833346d13f49263ff2a9b52ec67.png)
1.Cookie入门:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie cookie = new Cookie("key","value");
response.addCookie(cookie);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
String name = cookie.getName();
if ("username".equals(name)){
String value = cookie.getValue();
System.out.println(name+","+value);
break;
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/91302913d3447de8e68cf503692f3e7e.png)
Cookie存活时间&如何存储中文
- 发送请求
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String value = "张三";
value = URLEncoder.encode(value,"utf-8");
Cookie cookie = new Cookie("Username","张三");
response.addCookie(cookie);
}
2. 响应数据
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
String name = cookie.getName();
if ("username".equals(name)){
String value = cookie.getValue();
value = URLDecoder.decode(value,"utf-8");
System.out.println(name+","+value);
break;
}
}
}