HttpSessionBindingListener接口有两方需要实现的方法:
public synchronized void valueBound(HttpSessionBindingEvent httpsessionbindingevent)
public synchronized void valueUnbound(HttpSessionBindingEvent httpsessionbindingevent)
Session创建的时候Servlet容器将会调用valueBound方法;Session删除的时候则调用valueUnbound方法.
具体应用可以跟踪每个在登录用户,记录相关的信息,以及在其超时的时候及时的删除相关的信息.
实例代码如下:
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
....
....
public synchronized void valueBound(HttpSessionBindingEvent httpsessionbindingevent){
//检查是否有效
if(!Valid()) return;
//检查信息是否已经存在.不存在则添加.
if(Info.online.containsKey(m_userId))
{
//记录日志信息
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 httpsessionbindingevent){
//检查是否