java中cookie的元素无论如何也删不掉!跪求解答!

2024-11-26 02:34:14
推荐回答(1个)
回答1:

你可以这样删除Cookie
public boolean deleteCookie(String cookieName) {
if (cookieName != null) {
Cookie cookie = getCookie(cookieName);//getCookie()方法在下面
if (cookie != null) {
cookie.setMaxAge(0);// 如果0,就说明立即删除
cookie.setPath("/");// 不要漏掉
response.addCookie(cookie);
return true;
}
}
return false;
}
public Cookie getCookie(String cookieName) {
Cookie[] cookies = request.getCookies();
Cookie cookie = null;
try {
if (cookies != null && cookies.length > 0) {
for (int i = 0; i < cookies.length; i++) {
cookie = cookies[i];
if (cookie.getName().equals(cookieName)) {
return cookie;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
希望对你有所帮助!