设置Cookie
程序员文章站
2022-03-03 12:40:06
...
public static void setCookie(javax.servlet.http.HttpServletResponse response,String name,String value){
if (null==name || "".equals(name))
return;
if (null==value )
value="null";
Cookie cookie=new Cookie(name, value);
cookie.setMaxAge(300*60);
cookie.setDomain("url");
cookie.setPath("/");
response.addCookie(cookie);
}
删除Cookie
Cookie cookie =new Cookie("loginId", null);
cookie.setMaxAge(0); //设置为0就过期了。即删除。
cookie.setDomain("url");
cookie.setPath("/");
response.addCookie(cookie);
获取Cookie
public static String getHeadCookie(
javax.servlet.http.HttpServletRequest request, String cookie_name) {
String cookie_value = "";
Cookie cookies[] = request.getCookies();
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
System.out.println("cookies[i].getValue = "
+ cookies[i].getName());
if (cookies[i].getName().equals(cookie_name)) {
cookie_value = cookies[i].getValue();
}
}
}
return cookie_value;
}
上一篇: cookie自动登录(一月内不用登录)
下一篇: servlet实现简单下载(copy)