标签:
package com.core.util; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CookieUtil { private CookieUtil() {} public static final String COOKIE_PATH_ROOT = "/"; public static Cookie createCookie(String key, String value, int maxAge) { Cookie cookie = new Cookie(key, value); cookie.setPath(COOKIE_PATH_ROOT); if (maxAge > 0) { cookie.setMaxAge(maxAge); } return new Cookie(key, value); } public void addCookie(HttpServletResponse response, Cookie cookie) { response.addCookie(cookie); } public static Cookie getCookie(HttpServletRequest request, String cookieName) { Cookie[] cookies = request.getCookies(); if (null == cookies) { return null; } for (Cookie cookie : cookies) { if (cookieName.equals(cookie.getName())) { return cookie; } } return null; } public static String getCookieValue(HttpServletRequest request, String cookieName) { Cookie cookie = getCookie(request, cookieName); if (null != cookie) { return cookie.getValue(); } return null; } }
标签:
原文地址:http://www.cnblogs.com/shell-blog/p/5904380.html