1. SessionID号以Cookie的方式存到客户端,该ID号客户端和服务器端相同。
2.存储在服务器端。
3.不存在子路径、父路径问题。关键是有父子关系的一套窗口:这是同一个session。
4.如果禁用Cookie,则sessionid不能写入到客户端,每次都是新建的session。
如何解决?
方法:URL重写。
response.encodeURL(request.getRequestURI().toString());
若Cookie被禁止掉,则获取请求地址后,加上 session id
response:
String | encodeURL(String url) Encodes the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged. |
这就是encodeURL方法的功能。
request information:
StringBuffer | getRequestURL() Reconstructs the URL the client used to make the request. |
获取客户端此次请求的URL地址。
String | getRequestedSessionId() Returns the session ID specified by the client. |
这个是获取从客户端带来的sessionid。
boolean | isRequestedSessionIdFromCookie() Checks whether the requested session ID came in as a cookie. |
判断是否从Cookie中获取的sessionid,关键看cookie是否被禁用。
boolean | isRequestedSessionIdFromURL() Checks whether the requested session ID came in as part of the request URL. |
判断是否是通过URL重写得到的sessionid。
总结下:
1.session是服务器的一块内存,key-value对;
2.一套客户端窗口对应着一个session;
3.客户端和服务器端有对应的session id;
4.客户端向服务器端发送session id的方式:
第一:cookie没禁用 cookie中读取;
第二:cookie被禁用 URL重写。
5.为了安全使用session,应重写URL。