HttpSession

java web服务器通过实现httpsession来保存客户端的状态(jsessionid),也就是我们通常说的session。session是通过cookie机制来实现(网上说如果客户端禁用了cookie,则可以通过url重写来实现,一会再讨论。。)。
1、session的生成
在客户端第一次请求jsp页面,或servlet时生成,并向客户端写一个标识,即:jsessionid
由请求返回的http协议串可以看出:

Http代码 
1.请求:  
2.POST /ibsm/LoginAction.do HTTP/1.1 
3.Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*  
4.Referer: http://192.168.3.197:8080/ibsm/  
5.Accept-Language: zh-cn  
6.Content-Type: application/x-www-form-urlencoded  
7.UA-CPU: x86  
8.Accept-Encoding: gzip, deflate  
9.User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)  
10.Host: 192.168.3.197:8080 
11.Content-Length: 42 
12.Connection: Keep-Alive  
13.Cache-Control: no-cache  
14. 
15.code=lyc&password=123&actType=ywgl&userId=  
16. 
17.响应:  
18.HTTP/1.1 200 OK  
19.Server: Apache-Coyote/1.1 
20.Set-Cookie: JSESSIONID=1442A671BEEDA147A2756B7E083D3B7E; Path=/ibsm  
21.Content-Type: text/html;charset=GBK  
22.Content-Length: 436 
23.Date: Mon, 01 Feb 2010 05:18:06 GMT 
请求:
POST /ibsm/LoginAction.do HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: http://192.168.3.197:8080/ibsm/
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
Host: 192.168.3.197:8080
Content-Length: 42
Connection: Keep-Alive
Cache-Control: no-cache

code=lyc&password=123&actType=ywgl&userId=

响应:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=1442A671BEEDA147A2756B7E083D3B7E; Path=/ibsm
Content-Type: text/html;charset=GBK
Content-Length: 436
Date: Mon, 01 Feb 2010 05:18:06 GMT

由响应的第三行可以看出生成的标识:JSESSIONID=1442A671BEEDA147A2756B7E083D3B7E
2、session的使用
在session生成以后,以后请求时,都会自动发送上边生成的标识。浏览器后台发送的请求报文如下:

Http代码 
1.GET /ibsm/ApplicationFrame.frame HTTP/1.1 
2.Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*  
3.Accept-Language: zh-cn  
4.UA-CPU: x86  
5.Accept-Encoding: gzip, deflate  
6.User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)  
7.Host: 192.168.3.197:8080 
8.Connection: Keep-Alive  
9.Cookie: JSESSIONID=1442A671BEEDA147A2756B7E083D3B7E 
GET /ibsm/ApplicationFrame.frame HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
Host: 192.168.3.197:8080
Connection: Keep-Alive
Cookie: JSESSIONID=1442A671BEEDA147A2756B7E083D3B7E

可以看到最后一行就是生成的jsessionid
3、session的失效与销毁
a、session超时,这个依赖与服务器端的设置(web.xml中的配置session超时时间),过了这个时间,session将被销毁。
b、关闭浏览器,此时session不一定被销毁,但是已经失效,因为session只能在一个窗口内使用,(不讨论经其他手段在多个窗口中获取同一个session)。session的销毁要到超时才能自动销毁。
c、退出应用。如果在退出应用的方法中调用了销毁session的方法,则session被销毁。否则不会销毁,等待超时自动销毁。

你可能感兴趣的:(Httpsession)