Java Cookie工具类代码
1. 添加cookie(设置有效时间)
2. 检索所有cookie 封装到map集合 以其cookie name作为key cookie value作为value
3. 通过cookie name 获取 cookie value
import java.util.HashMap; import java.util.Map; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CookieTool { /** * 添加cookie * * @param response * @param name * @param value * @param maxAge 有效时间 */ public static void addCookie(HttpServletResponse response, String name, String value, int maxAge) { Cookie cookie = new Cookie(name, value); cookie.setPath("/"); cookie.setMaxAge(maxAge); cookie.setDomain(".189.cn"); // cookie作用域 response.addCookie(cookie); } /** * 检索所有cookie 封装到map集合 以其cookie name作为key cookie value作为value * * @param request * @return */ public static Map<String, String> ReadCookieMap(HttpServletRequest request) { Map<String, String> cookieMap = new HashMap<String, String>(); Cookie[] cookies = request.getCookies(); if (null != cookies) { for (Cookie cookie : cookies) { cookieMap.put(cookie.getName(), cookie.getValue()); } } return cookieMap; } /** * 通过cookie name 获取 cookie value * * @param request * @param name * @return */ public static String getCookieValueByName(HttpServletRequest request, String name) { Map<String, String> cookieMap = ReadCookieMap(request); if (cookieMap.containsKey(name)) { String cookieValue = (String) cookieMap.get(name); return cookieValue; } else { return null; } } }
未经允许请勿转载:程序喵 » 【Java】Cookie工具类代码