【问题标题】:Not getting session attributes in sessionDestroyed() method of HttpSessionListener未在 HttpSessionListener 的 sessionDestroyed() 方法中获取会话属性
【发布时间】:2013-10-03 12:39:45
【问题描述】:

我在 Google 上搜索和 stackoverflow 了很多,但无法让它工作。这是我的代码。 我在 subscribe 方法中设置了一个会话属性“topic”,但在 sessionDestroyed 中我将其设置为 null。 SO 上的This question 似乎与我的相关,但不能解决问题。

@Path("/jpubsub/{topic}")
    public class JMSSubscribe implements HttpSessionListener, ServletContextListener, MessageListener, ExceptionListener {
    
        @GET
        public subscribe(@javax.ws.rs.core.Context HttpServletRequest request) {
            HttpSession session = request.getSession();
            session.setAttribute("topic", "1");
        }
    
        @Override
        public void sessionCreated(HttpSessionEvent hse) {
            HttpSession session = hse.getSession();
                System.out.println("Created Session - ID : " + session.getId());
        }
    
        @Override
        public void sessionDestroyed(HttpSessionEvent hse) {
                System.out.println("Destroyed Session - ID : " + hse.getSession().getId());
                System.out.println("Topic ID sessionDestroyed - " + hse.getSession().getAttribute("topic"));
        }

请帮忙。

PS:当我在sessionCreated() 中设置属性时,我会在sessionDestroyed() 中得到它。 是因为我使用了不同的会话对象吗?另外,当我打印会话 ID 时。我在所有 3 种方法中都获得了相同的会话 ID。

请询问是否需要任何其他代码。

【问题讨论】:

  • 如果您对某个问题投反对票,请给出理由。
  • 你为什么将 SessionListener 与 JMS MessageListener 一起使用?

标签: java session httpsession


【解决方案1】:

在调用sessionDestroyed() 之后,会话中的所有对象都已经被清除了。因此,您得到空值。相反,您应该实现HttpSessionBindingListener 接口。

并且不要使用原始字符串对象存储在会话中,而是创建一个实现上述接口的简单对象。当它从会话中解除绑定(删除)时,您将获得该值。假设没有其他人删除它,它只会在 session 被实际销毁之前被调用。

【讨论】:

  • 当我调用 session.setAttribute("topic", "1");在 subscribe() 中,valueBound() 方法没有被调用
  • 因为“1”不是实现HttpSessionBindingListener的对象。请参考我的回答,您必须创建一个实现此接口的自定义对象。您必须在 Session 中设置此自定义对象。
  • @Bimlesh - '调用 sessionDestroyed() 后,会话中的所有对象都已被清除。' --> 这不是真的。当我在 sessionCreated 方法中设置属性时,我在 sessionDestroyed 中得到它。
  • @BimaleshJha:sessionDestroyed() 方法被调用只是 before 会话无效而不是之后。
  • 在 jboss 4.0 中嵌入的 tomcat 5.5 中,访问会话对象在 sessionDestroyed() 中返回 null。我认为它基于 servlet 2.3 规范。我不确定以后是否发生了变化。
【解决方案2】:

在会话创建方法中设置会话属性

@Path("/jpubsub/{topic}")
public class JMSSubscribe implements HttpSessionListener, ServletContextListener, MessageListener, ExceptionListener {

    @GET
    public subscribe(@javax.ws.rs.core.Context HttpServletRequest request) {
        HttpSession session = request.getSession();
    }

    @Override
    public void sessionCreated(HttpSessionEvent hse) {
        HttpSession session = hse.getSession();
        session.setAttribute("topic", "1");

            System.out.println("Created Session - ID : " + session.getId());
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent hse) {
            System.out.println("Destroyed Session - ID : " + hse.getSession().getId());
            System.out.println("Topic ID sessionDestroyed - " + hse.getSession().getAttribute("topic"));
    }

【讨论】:

  • 调用 sessionCreated 时我没有可用的数据。检查我的编辑。
  • "1" 实际上是一个动态存储的数据,只能在subscribe方法中使用。
  • 这正是 OP 已经说过的不起作用。被破坏的会话没有属性。 -1
  • @EJP 会话属性将在会话destoyed 被调用之前可用。你可以测试它。创建一个会话属性。尝试在会话销毁方法中打印它。然后调用 session.invalidate()。你将 100% 获得会话属性值。否则我会给你截图代码
  • 'Just before' 没有多大用处。 被调用时它们不可用。这就是问题的意义所在。您似乎甚至没有阅读过它。
猜你喜欢
  • 2011-09-21
  • 2015-06-29
  • 1970-01-01
  • 2023-03-09
  • 2013-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-18
相关资源
最近更新 更多