温馨提示:这篇文章已超过972天没有更新,请注意相关的内容是否还可用!
一、获取指定cookie
public static String getCookie(HttpServletRequest request,String key){ Cookie[] cookies = request.getCookies(); if(cookies != null){ for(Cookie cookie : cookies){ if(cookie.getName().equals(key)){ return cookie.getValue(); } } } return null; }
二、添加cookie
public static void removeCookie(HttpServletResponse response,String key,String value){ Cookie cookie = new Cookie(key,value); cookie.setPath("/"); cookie.setDomain("xxx.com"); cookie.setMaxAge(1 * 24 * 60 * 60);// 1天过期 response.addCookie(cookie); }
三、删除指定cookie
public static void removeCookie(HttpServletResponse response,String key){ Cookie cookie = new Cookie(key,"a"); cookie.setPath("/"); cookie.setDomain("xxx.com"); cookie.setMaxAge(0); response.addCookie(cookie); }
免责声明:本文来自左鹏,不代表0oD三一o0的观点和立场,如有侵权请联系本平台处理。
发表评论