/**
* 删除cookie
*/
public static void clearCookie(HttpServletRequest request,HttpServletResponse response, String path) {
Cookie[] cookies = request.getCookies();
try{
for(int i=0;i<cookies.length;i++) {
//System.out.println(cookies[i].getName() + ”:” + cookies[i].getValue());
Cookie cookie = new Cookie(cookies[i].getName(), null);
cookie.setMaxAge(0);
cookie.setPath(path);//根据你创建cookie的路径进行填写
response.addCookie(cookie);
}
}catch(Exception ex) {
System.out.println(“删除Cookies发生异常!”);
}
}
public static void setCookie(HttpServletResponse response, String name, String value, String path) {
if (logger.isDebugEnabled()) {
logger.debug(“Setting cookie ’” + name + “‘ on path ’” + path + “‘”);
}
Cookie cookie = new Cookie(name, value);
cookie.setSecure(false);
cookie.setPath(path);
cookie.setMaxAge(Constants.COOKIE_INVALID_TIME);
response.addCookie(cookie);
// logger.info(”setCookie 完成…….”);
}
//调用
String cookiename = “vbo”;
String cookievalue = “cb”;
String path = “/”;
setCookie(response, cookiename , cookievalue , path);
clearCookie(request,response,path);//如果有name的话,方法也要增加name过去才能匹配
- JAVA如何设置和删除COOKIE
看过该Java开发的还看过
本文标题:#JAVA如何设置和删除COOKIE#,宏鹏发布于北大青鸟鲁广校区。JAVA如何设置和删除COOKIE,根据你创建cookie的路径进行填写,删除Cookies发生异常处理。