tomcat jsession原理

1、浏览器第一次请求

浏览器cookie中没有jsessionid,所以http请求包cookie中无jsessionid的值

tomcat收到http请求,取出cookie中的jsessionid,没有取到值所以生成sessionid并保存到tomcat中, 且添加到request中,request.getSession().getId()可以获取sessionid。

response返回结果时会告诉浏览器设置jsessionid

2、浏览器第二次访问时

浏览器cookie中有jsessionid,所以http请求包cookie中会带上jsessionid的值,

tomcat收到http请求,取出cookie中jsessionid,和tomcat中保存的sessionid对比,如果一致则不处理,如果不一致则重新生成一个sessionid并保存到tomcat,且添加到request中的sessionid。

注:此时request.getSession().getId()取到的sessionid是tomcat新生成的id,

从cookie中取到的id是浏览器发送请求是request带过来的id。

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

// 从Cookie数据中遍历查找, 并取jsessionid

if (null != cookies && cookies.length > 0) {

for (Cookie cookie : cookies) {

if ("JSESSIONID".equals(cookie.getName())) {

//有, 直接返回

return cookie.getValue();

}

}

}

wireshark抓包:

HTTP/1.1

Host: 10.26.201.210

Connection: keep-alive

Accept: application/json, text/javascript, /; q=0.01

X-Requested-With: XMLHttpRequest

User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36

Referer: http://10.26.201.210/antimoneylaundering/login/loginIn

Accept-Encoding: gzip, deflate

Accept-Language: zh-CN,zh;q=0.9

Cookie: JSESSIONID=2A799C85B95F18D2E89854D99B95B8A3; CSESSIONID=a43ed30a1f81411d95fa1a8b251ef81b; user_Name=

HTTP/1.1 200 OK

Server: nginx/1.14.0

Date: Tue, 26 Jun 2018 07:56:26 GMT

Content-Type: text/html;charset=UTF-8

Content-Length: 54

Connection: keep-alive

Set-Cookie: JSESSIONID=e107642b95724c61aeb242d8dcf4f07b; Path=/

X-Content-Type-Options: nosniff

{"errorNo":"0","user_Name":"18368868043","switch":"0"}GET /antimoneylaundering/query/searchHomePage HTTP/1.1

转载于:https://my.oschina.net/u/3865831/blog/1835806

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值