技术开发 频道

解析COOKIE?

  【IT168 技术文章】关于COOKIE和SESSION的关系,一直没搞清楚。网上一搜COOKIE,普遍都有会话COOKIE和持久COOKIE的概念。  

        刚开始理解时,我也认为会有持久和会话这两种COOKIE。我认为,

        会话COOKIE就是用来存放SESSIONID的,并且只存在于

        浏览器内存,浏览器关闭后会话COOKIE就被删除;

  持久COOKIE就是用来存放其它信息,并且是在我们的本地硬盘里能看到的那种COOKIE。

  然后我写了个SERVLET试了一下。 

1 public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {  
2       
3     Cookie cookie = null;  
4       
5     PrintWriter out = response.getWriter();  
6     out.println("<html><body>");  
7       
8     Cookie[] cookies = request.getCookies();  
9       
10     // 如果没有COOKIE,新建一个COOKIE  
11     if (cookies == null) {  
12           
13         out.println("<b>cookies is null.</b></br>");  
14           
15         cookie = new Cookie("new", "1");  
16           
17         response.addCookie(cookie);  
18           
19     } else {  
20           
21         out.println("<b>cookies is not null.</b></br>");  
22           
23         for (int i = 0; i < cookies.length; i++) {  
24               
25             cookie = cookies[i];  
26               
27             out.println("cookie" + i + " name: " + cookie.getName() + "</br>");  
28         }  
29     }  
30       
31     HttpSession session = request.getSession();  
32       
33     if (session == null) {  
34         out.println("<b>session is null.</b></br>");  
35           
36     } else {  
37         out.println("<b>session is not null.</b></br>");  
38         out.println("session id: " + session.getId() + "</br>");  
39     }  
40       
41     out.println("</body></html>");  
42 }  
43

        第一次访问这个SERVLET,页面显示

  引用

1 cookies is null.
2 session is not null.
3 session id: 0D0AABB6F911362FEE87BEEB2953C33F
4

  第二次访问,页面显示

  引用

1 cookies is not null.
2 cookie0 name: new
3 cookie1 name: JSESSIONID
4 session is not null.
5 session id: 0D0AABB6F911362FEE87BEEB2953C33F

  第二次访问时从客户端来了两个COOKIE,名为“new”的COOKIE是我创建的,名为“JSESSIONID”的COOKIE应该是服务器TOMCAT创建的,但这时,在本地硬盘里找不到这两个COOKIE的文件。

  改一下SERVLET,在创建名为“new”的COOKIE时,加一句

1 cookie.setMaxAge(1000);  
2

  然后本地硬盘里就有“new”这个COOKIE了。

  这下就有疑问了,在没有设置COOKIE存活期时,自己创建的SESSION不会存到本地硬盘,会不会TOMCAT创建的所谓的会话COOKIE也是没有设置存活期呢?

  然后根据zddava的博客(http://zddava.javaeye.com/blog/311053),找到了TOMCAT里创建会话COOKIE的代码。

1 protected void configureSessionCookie(Cookie cookie) {
2
3   cookie.setMaxAge(-1);
4
5   String contextPath = null;
6
7   if (!connector.getEmptySessionPath() && (getContext() != null)) {
8
9   contextPath = getContext().getEncodedPath();
10
11   }
12
13   if ((contextPath != null) && (contextPath.length() > 0)) {
14
15   cookie.setPath(contextPath);
16
17   } else {
18
19   cookie.setPath("/");
20
21   }
22
23   if (isSecure()) {
24
25   cookie.setSecure(true);
26
27   }
28
29   }
30
31   protected void configureSessionCookie(Cookie cookie) {
32
33   cookie.setMaxAge(-1);
34
35   String contextPath = null;
36
37   if (!connector.getEmptySessionPath() && (getContext() != null)) {
38
39   contextPath = getContext().getEncodedPath();
40
41   }
42
43   if ((contextPath != null) && (contextPath.length() > 0)) {
44
45   cookie.setPath(contextPath);
46
47   } else {
48
49   cookie.setPath("/");
50
51   }
52
53   if (isSecure()) {
54
55   cookie.setSecure(true);
56
57   }
58
59   }
60
61

  cookie.setMaxAge(-1);使COOKIE在浏览器被关闭时删除。而且这里的cookie和上面SERVLET里的cookie都是javax.servlet.http.Cookie。

  由此得出结论,可能最开始是我理解错了,COOKIE并没有会话COOKIE和持久COOKIE之分。我们本地创建的“持久COOKIE”和WEB容器创建的“会话COOKIE”都是一种COOKIE,就是javax.servlet.http.Cookie。只是WEB容器把存活期设置成了关闭浏览器时删除而已(TOMCAT)。

0
相关文章