主要包括添加cookie
删除cookie
获取token
其他的,同学们根据实际需求去扩展修改吧。
public class CookieUtils {
//1年
public static final int default_age = 60 * 60 * 24 * 365;
public static final String domain = "localhost";
/**
* 设置cookie值
*
* @param response
* @param key
* @param value
*/
public static void setUpCookie(HttpServletResponse response, String key, String value) {
setUpCookie(response, key, value, default_age);
}
public static void setUpCookie(HttpServletResponse response, String key, String value, int age) {
Cookie cookie = new Cookie(key, value);
cookie.setPath("/");
cookie.setDomain(domain);
cookie.setMaxAge(age);
response.addCookie(cookie);
}
/**
* 删除cookie
*
* @param response
* @param key
*/
public static void deleteCookie(HttpServletResponse response, String key) {
setUpCookie(response, key, null, 0);
}
/**
* 获取cookie
*
* @param request
* @param key
* @return
*/
public static String getCookie(HttpServletRequest request, String key) {
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
if (key.equals(cookie.getName())) {
return cookie.getValue();
}
}
return null;
}
}