5.16
package com.xxxx.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;
@WebServlet("/c01")
public class Cookie01 extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie cookie = new Cookie("name","admin");
// 响应cookie对象
response.addCookie(cookie);
}
}
package com.xxxx.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("/c02")
public class Cookie02 extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
// 判断coookies是否为空
if(cookies != null && cookies.length >0){
for (Cookie cookie:cookies) {
String name = cookie.getName();
String value = cookie.getValue();
System.out.println("mingcheng:" + name + ",zhi:"+ value);
}
}
}
}
package com.xxxx.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("/c03")
public class Cookie03 extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*到期时间:负整数*/
Cookie cookie1 = new Cookie("name1", "value1");
cookie1.setMaxAge(-1);
response.addCookie(cookie1);
/*到期时间:正整数*/
Cookie cookie2 = new Cookie("name2", "value2");
cookie2.setMaxAge(20);
response.addCookie(cookie2);
/*到期时间:0*/
Cookie cookie3 = new Cookie("name3", "value3");
cookie3.setMaxAge(0);
response.addCookie(cookie3);
}
}
package com.xxxx.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;
import java.net.URLDecoder;
import java.net.URLEncoder;
//获取cookie
@WebServlet("/c04")
public class Cookie04 extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = "姓名";
String value = "张三";
// 将中文通过URLEncode进行编码
name = URLEncoder.encode(name);
value = URLEncoder.encode(value);
Cookie cookie = new Cookie(name, value);
response.addCookie(cookie);
// 将中文通过URLDecoder进行解码
Cookie[] cookies = request.getCookies();
// 判断非空
if(cookies != null && cookies.length >0){
for (Cookie c:cookies ) {
System.out.println(URLDecoder.decode(c.getName()));
System.out.println