The httpsessionbindinglistener interface has two methods to implement:
Public synchronized void valuebound (httpsessionbindingevent)
Public synchronized void valueunbound (httpsessionbindingevent)
When a session is created, the servlet container will call the valuebound method. When a session is deleted, the valueunbound method is called.
A specific application can track every logged-on user, record relevant information, and promptly Delete relevant information when it times out.
The instance code is as follows:
Import org. Apache. commons. Logging. log;
Import org. Apache. commons. Logging. logfactory;
....
....
Public synchronized void valuebound (httpsessionbindingevent ){
// Check whether it is valid
If (! Valid () return;
// Check whether the information already exists. If the information does not exist, add it.
If (info. Online. containskey (m_userid ))
{
// Record log information
Log.info ("initial user context ....");
Useronlinestruct UOS = (useronlinestruct) info. Online. Get (m_userid );
UOS. loginip = UOS. loginip;
UOS. logintime = UOS. logintime;
UOS. sessionid = UOS. sessionid;
}
Else {
Info. Online. Put (m_userid, UOS );
}
}
Public synchronized void valueunbound (httpsessionbindingevent ){
// Check whether it is valid
If (! Valid () return;
// Delete user information
// Record log information
Log.info ("destory user context ....");
Info. Online. Remove (m_userid );
}
--------------------------------
If you do not use log4j, you can change log.info () to system. Out. println () to get the same result.
If you want to configure log4j, click log usage
If you have any questions, please contact me: webmaster@bcxy.com