-session, or conversation, is a session state tracking technology in web development. Of course, the cookie described earlier is also a session-tracking technology. The difference is that the cookie saves the session state to the client, while session state is saved to the server side.
-So, what exactly is a "session"? When the user opens the browser, starting from the first request, until the browser is finally closed, the session is completed.
-the session is not the Javaweb development, but the technology used throughout the web development. In Javaweb development, the session is in the form of a Javax.servlet.http.HttpSession interface object.
1. Session Access:
A, the creation of the Session object:
javax.servlet.http. There are two methods in the HttpServletRequest interface, the GetSession () method, and the GetSession (Boolean Create) Method:
-GetSession () method: Used to return the current session associated with the request, or if there is no session in the request, create one and return.
-GetSession () method: Used to return the current session associated with the request, or if the passed parameter is true, if there is no session in the request, one is created and returned. (If the passed parameter is false, the session is not created and Null is returned.) )
650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/26/f6c0eda734774a5ed80a884fce9e89cf.png "title=" 00.png "alt=" F6c0eda734774a5ed80a884fce9e89cf.png "/>
2, the operation of the session domain property space:
-Session is a collection dedicated to storing data, and we generally call this memory space for storing data as domain attribute space, or domain. There are three methods in httpsession that are specifically used to write and read data in the domain's attribute space.
650) this.width=650; "src=" Https://s5.51cto.com/oss/201710/26/99595ad91fd72826b97a01f387435723.png "title=" 01.png "alt=" 99595ad91fd72826b97a01f387435723.png "/>
650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/26/6d0bede8c44488ff6b31e4dbe5e52520.png "style=" float: none; "title=" 02.png "alt=" 6d0bede8c44488ff6b31e4dbe5e52520.png "/>
650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/26/9d9ec7eca028d3543c30c6c678aa8866.png "style=" float: none; "title=" 03.png "alt=" 9d9ec7eca028d3543c30c6c678aa8866.png "/>
3. How the session works: in the server the system will maintain a session for each conversation. Different sessions, corresponding to a different session. So, how does the system identify each session object? That is how to do in the same session process, has been using the same session object?
A, write to the session list:
-the server manages the session in the current application in the form of a map, which is called the session list. The map key is a 32-bit-length random string, which is called Jsessionid,value and is a reference to the Session object.
-When the user submits the request for the first time, after the server servlet executes to the Request.getsession () method, A Map.entry object is automatically generated, and the key is a newly created HttpSession object based on a new jsessionid,value generated by an algorithm.
650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/26/8429a1b59371e61ce632312a0039f4ae.png "title=" 04.png "alt=" 8429a1b59371e61ce632312a0039f4ae.png "/>
B. The server generates and sends a cookie:
-After the session information is written to the session list, the system will also automatically "Jsessionid" as the name, the 32-bit length of the random string as value, stored in the form of a cookie in the response header, and with the response, The cookie is sent to the client.
650) this.width=650; "src=" Https://s2.51cto.com/oss/201710/26/043919100a31f3efaa07064c9bcdcba2.png "title=" 05.png "alt=" 043919100a31f3efaa07064c9bcdcba2.png "/>
C, the client receives and sends the cookie:
-The client receives this cookie and then stores it in the browser's cache. That is, cookies in the browser cache do not expire as long as the client browser does not close.
-When a user submits a second request, the cookie in the cache, along with the requested header information, is sent to the server
650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/26/c9d44326b3c5f48670843431b106f911.png "title=" 06.png "alt=" C9d44326b3c5f48670843431b106f911.png "/>
D. Find from the session list:
-the server reads the cookie from the request to the client and, based on the Jssessionid value of the cookie, finds the value corresponding to the key in the map, that is, the session object. Then, read and write the domain properties of the session object.
4. Expiration of Session:
-The concept of Session timeout introduced in web development, the expiration of the session refers to the timeout of the session. If a session has not been accessed for a specified time range, then the session will time out and expire.
-The time-out period of the session can be set by the <session-config> tab in Web. xml, in minutes. The default session time-out is 30 minutes. Again, this time is not the length of the life cycle from which the session was created, but the timing from the last access, the length of time that has not been accessed for the specified length of time.
650) this.width=650; "src=" Https://s2.51cto.com/oss/201710/26/b3744841b4a05779579644a0ef4115f5.png "title=" 07.png "alt=" B3744841b4a05779579644a0ef4115f5.png "/>
-If the timeout period is not reached, the session may be invalidated by the code in advance. Use
javax.servlet.http. The invalidate () method in the HttpSession interface:
650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/26/fbf4834a1481b59c99b4f2e034e19a87.png "title=" 08.png "alt=" Fbf4834a1481b59c99b4f2e034e19a87.png "/>
5. After the cookie is disabled, use session to track:
-from the working principle of the previous session, the server can find a different session for different sessions because the cookie completes the session tracking. However, if the client browser disables the cookie, how can the server ensure that the same session is used by the same conversation?
-If the client browser disables cookies, it will find every request submitted to the server, and the server will include a cookie with the name Jsessionid in its response, except that the cookie has a different value each time. That is, as long as the client browser submits a request that does not contain Jsessionid, the server considers this to be the beginning of a new session. A Map.entry object is generated for it, key is a new 32-bit-length random string, and value is a newly created session reference. In this case, session tracking is not possible.
-Additional Instructions:
A, domain attribute space range Comparison:
-In the Javaweb programming API, there are four spatial scope objects that can hold domain properties, and the domain properties stored in these four objects range from large to small, respectively:
-PageContext, HttpServletRequest, HttpSession, ServletContext,
-ServletContext, or application, the domain attribute placed in it is the entire application scope and can be completed across sessions sharing data.
-HttpSession, the domain property that is placed in is session-scoped and can be completed across requests to share data.
-HttpServletRequest, where the domain property is the request scope, you can complete the sharing of data across Servlets, but these servlets must be in the same request.
-PageContext, the domain properties placed in them are page-wide, not commonly used.
-The use of the three domain attribute space objects for the servletcontext,httpsession,httpservletrequest is based on the premise that the functional requirements can be guaranteed, with a small range of priority. This will not only save server memory, but also ensure the security of the data.
This article is from the "12392717" blog, please be sure to keep this source http://12402717.blog.51cto.com/12392717/1976574
HttpSession of the Servlet programming topic 7