/**
|
* Copyright © 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
*/
|
package com.javaweb.common.utils.cookie;
|
|
import javax.servlet.http.Cookie;
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.UnsupportedEncodingException;
|
import java.net.URLEncoder;
|
|
|
public class CookieUtils {
|
|
/**
|
* 设置 Cookie(生成时间为1天)
|
* @param name 名称
|
* @param value 值
|
*/
|
public static void setCookie(HttpServletResponse response, String name, String value) {
|
setCookie(response, name, value, 60*60*24);
|
}
|
|
/**
|
* 设置 Cookie
|
* @param name 名称
|
* @param value 值
|
*/
|
public static void setCookie(HttpServletResponse response, String name, String value, String path) {
|
setCookie(response, name, value, path, 60*60*24);
|
}
|
|
/**
|
* 设置 Cookie
|
* @param name 名称
|
* @param value 值
|
* @param maxAge 生存时间(单位秒)
|
*/
|
public static void setCookie(HttpServletResponse response, String name, String value, int maxAge) {
|
setCookie(response, name, value, "/", maxAge);
|
}
|
/**
|
* 保存
|
*
|
* @param response
|
* @param key
|
* @param value
|
* @param maxAge
|
*/
|
public static void setCookie(HttpServletResponse response, String key, String value, String domain, String path, int maxAge, boolean isHttpOnly) {
|
Cookie cookie = new Cookie(key, value);
|
if (domain != null) {
|
cookie.setDomain(domain);
|
}
|
cookie.setPath(path);
|
cookie.setMaxAge(maxAge);
|
cookie.setHttpOnly(isHttpOnly);
|
response.addCookie(cookie);
|
}
|
/**
|
* 设置 Cookie
|
* @param name 名称
|
* @param value 值
|
* @param maxAge 生存时间(单位秒)
|
* @param path 路径
|
*/
|
public static void setCookie(HttpServletResponse response, String name, String value, String path, int maxAge) {
|
Cookie cookie = new Cookie(name, null);
|
cookie.setPath(path);
|
cookie.setMaxAge(maxAge);
|
try {
|
cookie.setValue(URLEncoder.encode(value, "utf-8"));
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
response.addCookie(cookie);
|
}
|
|
/**
|
* 获得指定Cookie的值
|
* @param name 名称
|
* @return 值
|
*/
|
public static String getCookie(HttpServletRequest request, String name) {
|
Cookie cookie = get(request, name);
|
if (cookie != null) {
|
return cookie.getValue();
|
}
|
return null;
|
}
|
/**
|
* 查询Cookie
|
*
|
* @param request
|
* @param key
|
*/
|
private static Cookie get(HttpServletRequest request, String key) {
|
Cookie[] arr_cookie = request.getCookies();
|
if (arr_cookie != null && arr_cookie.length > 0) {
|
for (Cookie cookie : arr_cookie) {
|
if (cookie.getName().equals(key)) {
|
return cookie;
|
}
|
}
|
}
|
return null;
|
}
|
|
/**
|
* 删除Cookie
|
*
|
* @param request
|
* @param response
|
* @param key
|
*/
|
public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String key) {
|
Cookie cookie = get(request, key);
|
if (cookie != null) {
|
setCookie(response,key,"","/",0);
|
}
|
}
|
}
|