org.yelong.support.servlet.CookieUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yelong-support Show documentation
Show all versions of yelong-support Show documentation
对各种开源框架的包装、支持、拓展。这里也包含的yelong-core与orm框架的整合。
默认对所有依赖为 scope 为 provided 。您需要针对自己的需要进行再次依赖
/**
*
*/
package org.yelong.support.servlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author PengFei
*/
public final class CookieUtils {
private CookieUtils() {}
/**
* 获取cookie
*
* @param request request
* @param name cookie name
* @return cookie value
*/
public static final String getCookie(HttpServletRequest request,String name) {
Cookie[] cookies = request.getCookies();
if( null != cookies ) {
for (Cookie cookie : cookies) {
if(cookie.getName().equals(name)) {
return cookie.getValue();
}
}
}
return null;
}
/**
* 添加cookie
*
* @param response response
* @param name cookie name
* @param value cookie value
*/
public static final void addCookie(HttpServletResponse response,String name , String value) {
response.addCookie(new Cookie(name,value));
}
/**
* 移除 cookie
*
* @param response 响应
* @param name cookie name
*/
public static final void removeCookie(HttpServletResponse response ,String name) {
Cookie newCookie=new Cookie(name,null); //假如要删除名称为username的Cookie
newCookie.setMaxAge(0); //立即删除型
newCookie.setPath("/"); //项目所有目录均有效,这句很关键,否则不敢保证删除
response.addCookie(newCookie); //重新写入,将覆盖之前的
}
/**
* 清空cookie
* 将request中的所有cookie清除
*
* @param request request
* @param response response
*/
public static final void clearCookie(HttpServletRequest request ,HttpServletResponse response) {
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
removeCookie(response, cookie.getName());
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy