java delete cookie information _java how to set and delete cookies

/**

* delete cookies

*/

public static void clearCookie(HttpServletRequest request,HttpServletResponse response, String path) {

Cookie[] cookies = request.getCookies();

try{

for(int i=0;i

//System.out.println(cookies[i].getName() + ”:” + cookies[i].getValue());

Cookie cookie = new Cookie(cookies[i].getName(), null);

cookie.setMaxAge(0);

cookie.setPath(path);//Fill in the path where you created the cookie

response.addCookie(cookie);

}

}catch(Exception ex) {

System.out.println("Exception occurred while deleting 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认证

责编:xxm  评论 纠错

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324474439&siteId=291194637