2 回答
TA贡献1804条经验 获得超7个赞
方法如下
public void insertCookie(HttpServletRequest request,
HttpServletResponse response, Long appId) {
if (appId == null) {
appId = (long) -1;
}
this.cleanCookie(request, response, appId);
Cookie cookie = new Cookie("appId", "" + appId);
cookie.setPath("/");
response.addCookie(cookie);
}
public void cleanCookie(HttpServletRequest request,
HttpServletResponse response, long appId) {
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
if (cookie.getName().contains("" + appId)) {
String cookieName = cookie.getName();
Cookie newCookie = new Cookie(cookieName, null);
newCookie.setPath("/");
response.addCookie(newCookie);
}
}
}
TA贡献1804条经验 获得超8个赞
通过设置最大存活时间的形式来进行cookies注销删除。
举例:
Cookie NewCookie=new Cookie("newcookie",null); //将之前设置NewCookie值设置为空
NewCookie.setMaxAge(0); //设置存活时间为0.此时cookies就会自动注销
添加回答
举报