Set session Expiration Time
After the general system is logged in, it will set a time for the current session to expire to ensure that the user does not interact with the server for a long time, automatically exits the login, destroys the session (or the user exits the conversation)
There are three ways to set it up:
1. Set in the Web container (for Tomcat as an example)
Set in Tomcat-7.0\conf\web.xml, the following is the default configuration in tomcat7.0:
<session-config><session-timeout>30</session-timeout> </session-config>
<!--session-timeout The number of minutes--
Tomcat default session timeout is 30 minutes, can be modified as needed, negative or 0 is not limit session failure time
It is important to note that the time set by this session is calculated on the server, not the client. So if you are debugging a program, you should modify the server-side time to test instead of the client
PS: The actual test is not good, but can not say what is wrong, the feeling is not the session failed, but the session was lost (it is possible that the page left error and then lost the session?) No way.)
2. Set up in the project's Web. xml
<!--time in minutes--
<session-config><session-timeout>15</session-timeout></session-config>
3. Using Java Code settings
Session.setmaxinactiveinterval (30*60);//in seconds, i.e. after 30 minutes of inactivity, the session will expire
Three ways of priority: 1 < 2 < 3
In a general system, you may also need to do something after the session fails:
1. Control the number of users, when the session expires, the number of users of the system to reduce one, control the number of users within a certain range, to ensure the performance of the system
2. Control a user multiple login, when the session is valid, if the same user login, the prompt has been logged in, when the session expires, you can be different prompts, direct login
So how do you do a series of operations after the session fails?
There is a need to use the listener, that is, when the session fails for various reasons, the listener can listen to, and then execute the listener defined in the program is OK
Listener classes are: Httpsessionlistener class, with sessioncreated and sessiondestroyed two methods
You can inherit this class and then implement it separately.
Sessioncreated refers to the method that is executed when the session is created
Sessiondestroyed refers to the method that executes when the session fails
Example:
Public class Onlineuserlistener implements httpsessionlistener{ publicvoidsessioncreated (Httpsessionevent event) {HttpSession session=event.getsession; String ID=session.getid () +Session.getcreationtime (); SummerConstant.UserMap.put (id,boolean.true);//Add User} Publicvoidsessiondestroyed (Httpsessionevent event) {HttpSession session=event.getsession; String ID=session.getid () +Session.getcreationtime (); Synchronized ( This) {Summerconstant.usernum--;//number of users minus-SummerConstant.UserMap.remove (ID);//removed from the user group, the user group is a map } }}
Then just have to declare the listener in Web. Xml.
<listener>
<listener-class>com.demo.OnlineUserListener</listener-class>
</listener>
About Webapp--session Storage time