how to obtain cookies in Java
To retrieve cookies, you can use the getCookies() method of the HttpServletRequest object.
The example code is shown below:
HttpServletRequest request = ...; // 获取HttpServletRequest对象
// 获取Cookie数组
Cookie[] cookies = request.getCookies();
// 遍历Cookie数组,获取每个Cookie的名称和值
if (cookies != null) {
for (Cookie cookie : cookies) {
String name = cookie.getName();
String value = cookie.getValue();
System.out.println("Cookie Name: " + name + ", Value: " + value);
}
}
The above code first retrieves the HttpServletRequest object, then calls the getCookies() method to obtain an array of cookies. Next, by looping through the array of cookies, you can access the name and value of each cookie. Lastly, you can further manipulate the cookies as needed.