com.tmsps.ne4spring.utils.CookieUtil Maven / Gradle / Ivy
package com.tmsps.ne4spring.utils;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author zhangwei [email protected] CookieUtil
*/
public class CookieUtil {
private CookieUtil() {
}
// set cookie
public static void setCookie(HttpServletResponse response, String name, String value, int maxAgeInSeconds, String path, String domain, Boolean isHttpOnly) {
Cookie cookie = new Cookie(name, value);
cookie.setMaxAge(maxAgeInSeconds);
if (path == null) {
path = "/";
}
cookie.setPath(path);
if (domain != null) {
cookie.setDomain(domain);
}
if (isHttpOnly != null) {
cookie.setHttpOnly(isHttpOnly);
}
response.addCookie(cookie);
}
// get cookie
public static Cookie getCookieObject(HttpServletRequest request, String name) {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals(name)) {
return cookie;
}
}
}
return null;
}
public static String getCookie(HttpServletRequest request, String name) {
Cookie cookie = getCookieObject(request, name);
return cookie != null ? cookie.getValue() : "";
}
public static void setCookie(HttpServletResponse response, String name, String value, int maxAgeInSeconds) {
setCookie(response, name, value, maxAgeInSeconds, null, null, null);
}
}