关于CookieUtile的相关代码
public class CookieUtil{ /** * 根据name获得cookie * * @param request * @param name * @return */ public static Cookie getCookie(HttpServletRequest request,String name){ Cookie cookies[] = request.getCookies(); if (cookies == null || name == null || name.length() == 0){ return null; } for (int i = 0; i < cookies.length; i++){ if (name.equals(cookies[i].getName()) /* && request.getServerName().equals(cookies[i].getDomain()) */){ return cookies[i]; } } return null; } /** * 根据cookie对象移除cookie * * @param request * @param response * @param cookie */ public static void deleteCookie(HttpServletRequest request,HttpServletResponse response,Cookie cookie){ if (cookie != null){ cookie.setPath(getPath(request)); cookie.setValue(""); cookie.setMaxAge(0); response.addCookie(cookie); } } /** * 根据cookie的name删除 * * @param request * @param response * @param cookieName */ public static void deleteCookie(HttpServletRequest request,HttpServletResponse response,String cookieName){ Cookie cookie = new Cookie(cookieName, ""); deleteCookie(request, response, cookie); } /** * 通过name和value产生一个生命周期为2592000秒的cookie * * @param request * @param response * @param name * 键 * @param value * 值 */ public static void setCookie(HttpServletRequest request,HttpServletResponse response,String name,String value){ setCookie(request, response, name, value, 0x278d00); } /** * 通过name和value产生一个生命周期为maxAge秒的cookie * * @param request * @param response * @param name * 键 * @param value * 值 * @param maxAge * 生命周期 */ public static void setCookie(HttpServletRequest request,HttpServletResponse response,String name,String value,int maxAge){ Cookie cookie = new Cookie(name, value == null ? "" : value); cookie.setMaxAge(maxAge); cookie.setPath(getPath(request)); response.addCookie(cookie); } private static StringBuffer generateCookieStringInTomcat(final Cookie cookie,final boolean httpOnly){ final StringBuffer sb = new StringBuffer(); // web application code can receive a IllegalArgumentException // from the appendCookieValue invokation if (SecurityUtil.isPackageProtectionEnabled()){ AccessController.doPrivileged(new PrivilegedAction(){ public Object run(){ ServerCookie.appendCookieValue( sb, cookie.getVersion(), cookie.getName(), cookie.getValue(), cookie.getPath(), cookie.getDomain(), cookie.getComment(), cookie.getMaxAge(), cookie.getSecure(), httpOnly); return null; } }); }else{ ServerCookie.appendCookieValue( sb, cookie.getVersion(), cookie.getName(), cookie.getValue(), cookie.getPath(), cookie.getDomain(), cookie.getComment(), cookie.getMaxAge(), cookie.getSecure(), httpOnly); } return sb; } /** * setCookie的增强版 * * @param request * @param response * @param name * 键 * @param value * 值 * @param maxAge * 生命周期 */ public static void setCookieHttp(HttpServletRequest request,HttpServletResponse response,String name,String value,int maxAge){ StringBuffer cookie = generateCookieStringInTomcat(new TimeIntervalCookie(name, value, maxAge), true); response.addHeader("Set-Cookie", cookie.toString()); } private static String getPath(HttpServletRequest request){ String path = request.getContextPath(); return (path == null || path.length() == 0) ? "/" : path; }}