如何通过java清除cookie
答案:1 悬赏:20 手机版
解决时间 2021-01-27 14:38
- 提问者网友:难遇难求
- 2021-01-27 05:31
如何通过java清除cookie
最佳答案
- 五星知识达人网友:刀戟声无边
- 2021-01-27 05:44
方法如下
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);
}
}
}
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);
}
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯