From: http://blog.csdn.net/laolai14/article/details/3707087
8.1. Use httpsessionlistener
Compile an onlineuserlistener.
Package Anni;
Import java. util. List;
Import javax. servlet. servletcontext;
Import javax. servlet. http. httpsession;
Import javax. servlet. http. httpsessionlistener;
Import javax. servlet. http. httpsessionevent;
Public class onlineuserlistener implements httpsessionlistener {
Public void sessioncreated (httpsessionevent event ){
}
Public void sessiondestroyed (httpsessionevent event ){
Httpsession session = event. getsession ();
Servletcontext application = session. getservletcontext ();
// Obtain the logon Username
String username = (string) Session. getattribute ("username ");
// Delete the user name from the online list
List onlineuserlist = (list) application. getattribute ("onlineuserlist ");
Onlineuserlist. Remove (username );
System. Out. println (username + "times out and exits. ");
}
}
Onlineuserlistener implements two methods defined by httpsessionlistener: sessioncreated () and sessiondestroyed (). These two methods can be used to monitor the creation and destruction of sessions in the current application. Here we only use sessiondestroyed () to perform operations when the session is destroyed.
Get the session to be destroyed from httpsessionevent, get the username in the session, and delete it from the online list. The last line prints a message to the console, prompting that the operation is successful. This is only for debugging purposes and can be deleted during normal operation.
To make the listener function, we add it to Web. xml:
<listener>
<listener-class>anni.OnlineUserListener</listener-class>
</listener>
In the following two cases, the sessiondestoryed (Session destruction) event occurs:
When the session. invalidate () method is executed.
Since logoutservlet. execute session in Java. during invalidate (), sessiondestory () is triggered to clear the current user from the online user list. the online list is operated in Java, so logoutservlet. the Java content is now like this.
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
// Destroy the session
Request. getsession (). invalidate ();
// Succeeded
Response. sendredirect ("index. jsp ");
}
If the user does not access the server for a long time and exceeds the maximum Session Timeout time, the server will automatically destroy the timeout session.
You can set the Session Timeout value in Web. xml. To make it easy to see the timeout effect, we set the timeout value to the minimum value.
<session-config>
<session-timeout>1</session-timeout>
</session-config>
The unit of time is one minute and can only be an integer. If it is zero or negative, the session will never time out.
The corresponding example is in 08-01. to verify whether the onlineuserlistener can be executed normally, we can log on to two users, one of which is logged out, and the other is waiting for one minute. Then we can see the output information in the console.
470) This. style. width = 470 "align = center> 8.2. Use httpsessionbindinglistener
Although httpsessionbindinglistener is called a listener, its usage is completely different from that of httpsessionlistener. Let's take a look at how it is used.
Our onlineuserbindinglistener implements the httpsessionbindinglistener interface. The interface defines two methods: valuebound () and valueunbound (), which correspond to Data Binding and unbinding events respectively.
To bind session data, call session. setattribute () to save httpsessionbindinglistener to the session. We will perform this step in loginservlet. java.
// Put the user name in the online list
Session. setattribute ("onlineuserbindinglistener", new onlineuserbindinglistener (username ));
This is the biggest difference between httpsessionbindinglistener and httpsessionlistener: httpsessionlistener only needs to be set to Web. XML to listen to all sessions in the entire application. Httpsessionbindinglistener must be instantiated and put into a session for listening.
Compared to the listening range, httpsessionlistener can listen to all sessions once. httpsessionbindinglistener is usually one-to-one.
This difference makes httpsessionbindinglistener advantageous. We can make each listener correspond to a username, so that we do not need to read username in the session every time, the Code of all online list operations can be moved to listener, which is easier to maintain.
The code for the valuebound () method is as follows:
Public void valuebound (httpsessionbindingevent event ){
Httpsession session = event. getsession ();
Servletcontext application = session. getservletcontext ();
// Put the user name in the online list
List onlineuserlist = (list) application. getattribute ("onlineuserlist ");
// Initialization is required before use for the first time.
If (onlineuserlist = NULL ){
Onlineuserlist = new arraylist ();
Application. setattribute ("onlineuserlist", onlineuserlist );
}
Onlineuserlist. Add (this. username );
}
The username has been passed to the listener through the constructor. When binding data, you can directly put it into the user list.
The corresponding valueunbound () method. The Code is as follows:
Public void valueunbound (httpsessionbindingevent event ){
Httpsession session = event. getsession ();
Servletcontext application = session. getservletcontext ();
// Delete the user name from the online list
List onlineuserlist = (list) application. getattribute ("onlineuserlist ");
Onlineuserlist. Remove (this. username );
System. Out. println (this. username + "exit. ");
}
Here, you can directly use the username of listener to operate the online list without worrying about the existence of username in the session.
The trigger conditions of valueunbound are as follows:
When session. invalidate () is executed.
The session times out and is automatically destroyed.
Run session. setattribute ("onlineuserlistener", "other objects"); or session. removeattribute ("onlineuserlistener") to delete the listener from the session.
Therefore, as long as the listener is not deleted from the session, the session can be destroyed.
Bytes ----------------------------------------------------------------------------------------
The sessiondestroyed of httpsessionlistener is executed only when the session times out and httpsession. invalidate is called.