/**
* cookie工具类
* @author Stodger
* @version V1.0
*/
public class CookieUtil {
/**
* 获取cookie值
* @param request
* @param cookieName 名称
* @return
*/
public static String getCookieValue(HttpServletRequest request, String cookieName){
Cookie[] cookies = request.getCookies();
if(cookies != null){
for(Cookie cookie : cookies){
if(cookie.getName().equals(cookieName)){
return cookie.getValue();
}
}
}
return null;
}
/**
* 设置cookie
* @param response
* @param cookieName 名称
* @param value 值
* @param cookieMaxAge 存活时间
*/
public static void setCookie(HttpServletResponse response, String cookieName, String value,int cookieMaxAge){
Cookie cookie = new Cookie(cookieName,value);
cookie.setPath("/");
cookie.setMaxAge(cookieMaxAge);
response.addCookie(cookie);
}
/**
* 移除cookie
* @param request
* @param response
* @param name 名称
*/
public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String name) {
if (null == name) {
return;
}
setCookie(response,name,null,0);
}
}