Session usage, session usage
1. What is session?
Session: it is called "Session control" in computers, especially in network applications ". The Session object stores the attributes and configuration information required for a specific user Session. In this way, when a user jumps between Web pages of an application, the variables stored in the Session object will not be lost, but will continue to exist throughout the user Session. When a user requests a Web page from an application, if the user does not have a Session, the Web server automatically creates a Session object. When a session expires or is abandoned, the server terminates the session. The most common usage of Session objects is to store users' preferences.
Sessionid when you access a website for the first time, the website server will add the Set-Cookie JSESSIONID = nj1tvkclp3000083olcn3191sjq3 (java Server) Information in the Response Header, Which is randomly generated by the server, put it in the server memory. in order to identify a unique client user, the content will not be repeated. This is sessionid.
Ii. How sessions work
1. session Lifecycle
- When a customer visits a page on the server for the first time, the server will allocate a session object to the user and specify a unique ID for the session, in addition, this ID is sent to the client and written to the cookie, so that the client and the server session have a one-to-one correspondence relationship;
- When the client continues to access other resources on the server, the server no longer assigns a new session object to the client until the client browser closes, times out, or calls the invalidate () method of the session to invalidate it, the session between the client and the server ends.
- When the customer re-opens the browser to access the website, the server will re-allocate a session object to the customer and re-allocate the sessionID.
-
2. Reasons for session failure
- The session itself has a survival time. The default time in tomcat is 30 minutes, which has nothing to do with the browser, because even if your browser is always open, if no request is sent within 30 minutes, then there will be nothing in the session domain on your server. When you access the server again, the server will create a new session. The session ID is used to determine whether a new session is used. The change of session time is changed through session. getMaxInactiveInterval.
- Call invalidate ()
- Server restart or interruption
-
Note:
The browser places the sessionid in its own process memory. Different browsers here may be different. IE processes cannot share this sessionid, that is, new IE will not be able to share this sessionid; firefox processes can be shared. then, when you send a request to the website, the browser will put the sessionid in the request header and send it to the server, in this way, the server gets the sessionid and then compares it with the sessionid stored in its memory to lock the client, so as to distinguish different clients and complete the session.
It can be seen that if this method is used, when the user closes the browser and ends the process during the session, the session will disappear. If the user opens the browser again and wants to continue the session, the server will not be able to tell the session information to the server because the sessionid is not included in the sent request. Note: (at this time, the session ID and sessionid of the server end still exist, only the correct sessionid does not match it and occupies the server memory. The memory is released only when the session expires or the server restarts ).
Iii. session usage
1. session Method
- Public void setAttribute (String name, String value) sets the attribute value of the specified name.
- Public Object getAttribute (String name) obtains the attribute value of the specified name within the session range.
- Public void removeAttribute (String name): deletes the session attribute of the specified name. If this attribute does not exist, an exception occurs.
- Public void invalidate () to invalidate the session. The current session can be invalidated immediately. All objects stored in the original session cannot be accessed again.
- Public String getId () to obtain the current session ID. Each session has a unique sessionID on the server side. The unique data sent from the session object to the browser is sessionID, which is generally stored in cookies.
- Public void setMaxInactiveInterval (int interval) sets the maximum session duration in seconds. A negative number indicates that the session will never expire.
- Public int getMaxInActiveInterval () to get the maximum session duration. If the value is-1, it will never expire and will only expire when the server is restarted.
- The getCreationTime () and getLastAccessedTime () Methods of the session object can be used to obtain the session creation time and the last access time, but the returned value is millisecond, the following conversions are generally used to obtain the specific date and time.
2. There are two methods to obtain the session object.
(1) first, it is implemented through the HttpServletRequest object.
- Reques. getSession (true) is equivalent to request. getSession (); means that if a session exists, the session is returned. Otherwise, a new session is created and a new session is returned.
- Request. getSession (false) means that if this session exists, the session is returned; otherwise, null is returned.
Note: A session needs to occupy server resources. Unless the session does not exist or must be created, it is generally best to use request. getSession (false ). Under normal circumstances, you can create a parameter in the session and determine whether the session exists by obtaining whether the parameter is null. If it is null, no session exists. Otherwise, the session exists.
Private boolean ifExistsSession (HttpServletRequest request) {HttpSession session = request. getSession (false); if (session! = Null) {// get the variable if (session. getAttribute ("name") created in sesison ")! = Null) {return true;} return false ;}
(2) obtain by using the pagecontext object xt, pageContext. session ();
In line with the principle of summing up and sharing learning experience, please correct me if you have any mistakes