Java 将对象与http会话关联
在我的java web应用程序(tomcat8,servlet3)中,我有一个Java 将对象与http会话关联,java,session,tomcat,servlets,web,Java,Session,Tomcat,Servlets,Web,在我的java web应用程序(tomcat8,servlet3)中,我有一个列表ctx我的实体类,我用我的上下文(列表)为每个会话(用户)保留一个实例,并在用户会话中保留一个引用副本,如下所示 javax.servlet.http.HttpSession sess=request.getSession(true); //declaring and initializing the entity object entity e=new entity(); e.timestamp=System.c
列表ctx
我的实体
类,我用我的上下文(列表)为每个会话(用户)保留一个实例,并在用户会话中保留一个引用副本,如下所示
javax.servlet.http.HttpSession sess=request.getSession(true);
//declaring and initializing the entity object
entity e=new entity();
e.timestamp=System.currentTimeMillis();
//keep the e with the session
sess.setAttribute("e",e);
//and a reference copy with another context
ctx.add(e);
问题:我只想知道(事件、侦听器等)有关用户http会话的信息,以便在会话过期(删除)时从
ctx
中删除实体
对象。现在我如何才能意识到会话已从服务器过期?
只需使用
HttpSessionListener
。它是专门为这种用途而存在的。只需覆盖sessionDestroyed(HttpSessionEvent se):
别忘了申报。Extract from javadoc:为了接收这些通知事件,必须在web应用程序的部署描述符中声明实现类,使用WebListener进行注释,或者通过ServletContext上定义的addListener方法之一进行注册
@Override
public void sessionDestroyed(HttpSessionEvent se) {
HttpSession session = se.getSession();
// do your processing
}
@Override
public void sessionCreated(HttpSessionEvent se) {
// empty implementation if you do not need it ...
}