com.webapp.utils.servlet.CookieUtils Maven / Gradle / Ivy
package com.webapp.utils.servlet;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
public final class CookieUtils {
public static String getCookieByName(HttpServletRequest request, String name) {
Map cookieMap = readCookieMap(request);
if (cookieMap.containsKey(name)) {
Cookie cookie = (Cookie) cookieMap.get(name);
return cookie.getValue();
} else {
return null;
}
}
public static boolean hasCookie(HttpServletRequest request, String name) {
Map cookieMap = readCookieMap(request);
return cookieMap.containsKey(name);
}
protected static Map readCookieMap(
HttpServletRequest request) {
Map cookieMap = new HashMap();
Cookie[] cookies = request.getCookies();
if (null != cookies) {
for (int i = 0; i < cookies.length; i++) {
cookieMap.put(cookies[i].getName(), cookies[i]);
}
}
return cookieMap;
}
}