一个HttpSession对应同一个客户端发送的多个Request。
一、SessionID
服务器通过SessionID来识别request是否来自同一个客户端。当第一次收到客户端发来的Requst时,Container会生成一个SessionID并随response发回客户端,当客户端发送随后的请求时将SessionID一同发送到服务器,Container将收到的SessionID与现存Session的SessionID进行匹配。
1、Cookies
Cookies是一种简单的发送SessionID的方法。当Container通过Cookies发送ID时,response header中会有Set-Cookie:JSESSIONID=1231231项,客户发回的request header中也会有Cookie:JSESSIONID=1231231项。
使用HttpRequest.getSession()会自动在response中加入Cookie:JSESSIONID,使用HttpSession.isNew()可以判断得到的Session是否是新的,当该Session中无匹配ID时视为新的,返回true。当想要获得旧的Session时,可以用HttpRequest.getSession(false),如果没有旧的Session则返回null。