【问题标题】:Associate an object with http session j2ee将对象与 http 会话 j2ee 关联
【发布时间】:2014-10-27 14:49:30
【问题描述】:

我的 Java Web 应用程序(tomcat8,servlet3)中有一个 entity 类的 List<entity> 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 中删除 entity 对象。
现在我怎么能意识到会话正在从服务器过期?

【问题讨论】:

    标签: java session tomcat servlets web


    【解决方案1】:

    只需使用HttpSessionListener。它专门用于该用途。只需覆盖 sessionDestroyed(HttpSessionEvent se)

    @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 ...
    }
    

    不要忘记声明它。从 javadoc 中提取:为了接收这些通知事件,实现类必须在 Web 应用程序的部署描述符中声明,使用 WebListener 进行注释,或者通过 ServletContext 上定义的 addListener 方法之一进行注册 .

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      • 2015-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-10
      • 1970-01-01
      相关资源
      最近更新 更多