设置Cookie

来源:互联网 发布:js导出excel代码 编辑:程序博客网 时间:2024/05/16 08:25
转自:http://hi.baidu.com/shengch_sky/blog/item/802f646291d3b64eebf8f89c.html
1.设置Cookie

  1 Cookie cookie = new Cookie("key", "value");

  2 cookie.setMaxAge(60); //设置60秒生存期,如果设置为负值的话,则为浏览器进程Cookie(内存中保存),关闭浏览器就失效。

  3 getPath/setPath获取/设置Cookie适用的路径。如果不指定路径,Cookie将返回给当前页面所在目录及其子目录下的所有页面。这里的方法可以用来设定一些更一般的条件。例如,someCookie.setPath("/"),此时服务器上的所有页面都可以接收到该Cookie。

  4 response.addCookie(cookie);

  2.读取Cookie

  1 1//该方法可以读取当前路径以及“直接父路径”的所有Cookie对象,如果没有任何Cookie的话,则返回null

  2 2Cookie[] cookies = request.getCookies();

  3.删除Cookie

  1 Cookie cookie = new Cookie("key", null);

  2 cookie.setMaxAge(0); //设置为0为立即删除该Cookie

  3 cookie.setPath("/test/test2"); //删除指定路径上的Cookie,不设置该路径,默认为删除当前路径Cookie

  4 response.addCookie(cookie);

  4.注意:假设路径结构如下

  /

  /test

  /test/test2

  /test345

  /test555/test666

  a.相同键名的Cookie(值可以相同或不同)可以存在于不同的路径下。

  b. 删除时,如果当前路径下没有键为"key"的Cookie,则查询全部父路径,检索到就执行删除操作(每次只能删除一个与自己最近的父路径Cookie)

  FF.必须指定与设定cookie时使用的相同路径来删除改cookie,而且cookie的键名不论大写、小写或大小混合都要指定路径。

  IE.键名小写时,如果当前路径为/test/test2,如果找不到再向上查询/test、/test555、/test345,如果还找不到就查询/ 。(/test555/test666不查询)

  键名大小写混合或大写时,不指定路径则默认删除当前路径,并且不向上查询。

  c.读取Cookie时只能读取直接父路径的Cookie。

  如果当前路径为/test/test2,要读取的键为"key"。当前路径读取后,还要读取/test,/test读取后,还要读取/

  d.在做Java的web项目时,由于一般的Web服务器(如Tomcat或Jetty)都用Context来管理不同的Web Application,这样对于每个Context有不同的Path,

  在一个Server中有多个Web Application时要特别小心,不要设置Path为/的Cookie,容易误操作。(当然前提是域名相同)

示例:

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {

public static Cookie getCookie(HttpServletRequest request, String name) {
Cookie cookies[] = request.getCookies();
if (cookies == null || name == null || name.length() == 0) {
return null;
}
for (int i = 0; i < cookies.length; i++) {
if (name.equals(cookies[i].getName())
&& request.getServerName().equals(cookies[i].getDomain())) {
return cookies[i];
}
}
return null;
}

public static void deleteCookie(HttpServletRequest request,
HttpServletResponse response, Cookie cookie) {
if (cookie != null) {
cookie.setPath(getPath(request));
cookie.setValue("");
cookie.setMaxAge(0);
response.addCookie(cookie);
}
}

public static void setCookie(HttpServletRequest request,
HttpServletResponse response, String name, String value) {
setCookie(request, response, name, value, 0x278d00);
}

public static void setCookie(HttpServletRequest request,
HttpServletResponse response, String name, String value, int maxAge) {
Cookie cookie = new Cookie(name, value == null ? "" : value);
cookie.setMaxAge(maxAge);
cookie.setPath(getPath(request));
response.addCookie(cookie);
}

private static String getPath(HttpServletRequest request) {
String path = request.getContextPath();
return (path == null || path.length()==0) ? "/" : path;
}

}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 快递被别人拆了怎么办 淘宝买的东西包装破损怎么办 寄血液被退回来怎么办 快递被安检扣了怎么办 淘宝原单退回运费怎么办 运输过程中包裹破损怎么办 天猫没收到货签收怎么办 收到的快递坏了怎么办 自寄的快递少了怎么办 邮的东西弄坏了怎么办 物流签收后发现货物损坏怎么办 发现客人损坏了酒店物品怎么办 东西坏了签收了怎么办 朋友圈贩卖三无产品你怎么办 付钱给微商没有保障怎么办 电镀锌钢带生锈怎么办 电机机油从空气滤芯里流出怎么办 把塑料皮套吃了怎么办 塑料框眼镜有点小了怎么办 出口纸箱打了钉怎么办 买房子交款单据丢了怎么办 买房子所有单据丢失怎么办 车险单据都丢了怎么办 真空包装的东西里面有空气怎么办 发货物忘记写唛头了怎么办 Word文档撤销按钮删除了怎么办 ai保存时未响应怎么办 ai还没保存卡了怎么办 ai卡住了没保存怎么办 屁股沟有硬块红肿怎么办 卧室床选太大了怎么办 画板的笔尖掉了怎么办 宜家水壶盖子有水怎么办 背滤鱼缸除油膜怎么办 书多了没地方放怎么办 学生在教室内丢手机怎么办 新车尾箱坏了怎么办 放书的箱子烂了怎么办 车钥匙锁后备箱里怎么办 布的收纳箱有味怎么办 车漆清漆层掉了怎么办