如何通过java清除cookie?

如何通过java清除cookie


MM们
浏览 1062回答 2
2回答

三国纷争

方法如下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);}}}

胡说叔叔

通过设置最大存活时间的形式来进行cookies注销删除。举例:Cookie NewCookie=new Cookie("newcookie",null); //将之前设置NewCookie值设置为空NewCookie.setMaxAge(0); //设置存活时间为0.此时cookies就会自动注销
打开App,查看更多内容
随时随地看视频慕课网APP