Java tutorial
/* * Copyright 2005-2012 shopxx.net. All rights reserved. * Support: http://www.shopxx.net * License: http://www.shopxx.net/license */ package net.shopxx.util; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils; import org.springframework.util.Assert; /** * Util - Cookie * http://www.shopxx.net * * Copyright 2012 shopxx.net All Rights Reserved. * License: http://www.shopxx.net/license * * Revision: 2.1 * Date: 2012-05 */ public final class CookieUtils { /** * ?? */ private CookieUtils() { } /** * cookie * * @param request * HttpServletRequest * @param response * HttpServletResponse * @param name * cookie?? * @param value * cookie * @param path * * @param maxAge * (??: ) * @param domain * * @param secure * ?? */ public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value, String path, Integer maxAge, String domain, Boolean secure) { Assert.notNull(request); Assert.notNull(response); Assert.hasText(name); try { value = URLEncoder.encode(value, "UTF-8"); Cookie cookie = new Cookie(name, value); if (StringUtils.isNotEmpty(path)) { cookie.setPath(path); } if (maxAge != null) { cookie.setMaxAge(maxAge); } if (StringUtils.isNotEmpty(domain)) { cookie.setDomain(domain); } if (secure != null) { cookie.setSecure(secure); } response.addCookie(cookie); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } /** * cookie * * @param request * HttpServletRequest * @param response * HttpServletResponse * @param name * cookie?? * @param value * cookie */ public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value) { addCookie(request, response, name, value, null, null, null, null); } /** * cookie * * @param request * HttpServletRequest * @param response * HttpServletResponse * @param name * cookie?? * @param value * cookie * @param path * */ public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value, String path) { addCookie(request, response, name, value, path, null, null, null); } /** * ?cookie * * @param request * HttpServletRequest * @param name * cookie?? * @return ?null */ public static String getCookie(HttpServletRequest request, String name) { Assert.notNull(request); Assert.hasText(name); Cookie[] cookies = request.getCookies(); if (cookies != null) { try { for (Cookie cookie : cookies) { if (name.equals(cookie.getName())) { return URLDecoder.decode(cookie.getValue(), "UTF-8"); } } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return null; } /** * cookie * * @param request * HttpServletRequest * @param response * HttpServletResponse * @param name * cookie?? * @param path * * @param domain * */ public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String name, String path, String domain) { Assert.notNull(request); Assert.notNull(response); Assert.hasText(name); Cookie cookie = new Cookie(name, null); cookie.setMaxAge(0); if (StringUtils.isNotEmpty(path)) { cookie.setPath(path); } if (StringUtils.isNotEmpty(domain)) { cookie.setDomain(domain); } response.addCookie(cookie); } /** * cookie * * @param request * HttpServletRequest * @param response * HttpServletResponse * @param name * cookie?? */ public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String name) { removeCookie(request, response, name, null, null); } /** * cookie * * @param request * HttpServletRequest * @param response * HttpServletResponse * @param name * cookie?? * @param path * */ public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String name, String path) { removeCookie(request, response, name, path, null); } }