会话超时时如何调用 sessionDestroyed [英] How to call sessionDestroyed when a session times out

查看:86
本文介绍了会话超时时如何调用 sessionDestroyed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 JSP 的新手.我在实现 HttpSessionListener 的类中使用了以下代码以在会话超时时获取 SESSION OUT:

I am new to JSP. I used the following code in a class that implements HttpSessionListener to get SESSION OUT when the session times out:

    public void sessionCreated(HttpSessionEvent arg0) {  
        System.out.print("SESSION Created");
    }

    public void sessionDestroyed(HttpSessionEvent arg0) {
        System.out.print("SESSION OUT");
    }

我在 web.xml 中设置:

  <session-config>
     <session-timeout>1</session-timeout> 
  </session-config>

servlet 等待两分钟以上,然后调用 sessionDestroyed.

The servlet waits more than two minutes, then it calls sessionDestroyed.

有没有办法在超时时强制sessionDestroyed?

Is there is a way to force sessionDestroyed when time out ?

提前致谢.

推荐答案

是什么让您认为 sessionDestroyed 在会话超时时不会被调用?或者换句话说,你对它被调用的事实有什么解释?

What makes you think that sessionDestroyed is not being called when the session times out? Or in other words, what's you interpretation of the fact it gets called at all?

servlet 将以自己的方式处理会话的有效性,包括会话超时,并且当它确定会话不再有效时,它将调用您的方法.但是,我认为在这方面没有任何 servlet 保证任何特定的及时性;我的理解是,它有点像垃圾收集,因为它保证在某个时间发生,但不一定在会话有资格销毁的最早实例中发生.

The servlet will handle the validity of sessions in its own manner, including session timeouts, and when it determines that the session is no longer valid it will call your method. However, I don't think any servlets guarantee any particular timeliness in this regard; my understanding is that it's a bit like garbage collection in that it is guaranteed to happen at some point, but not necessarily at the earliest possible instance that the session becomes eligible for destruction.

在任何情况下,似乎几乎可以肯定 servlet 正在执行您想要的操作 - 看到会话超时并调用适当的方法 - 唯一的问题是您是否会在 60 秒之后看到这一点最后一个请求或稍后.我建议一般来说你不应该依赖调用这个方法的确切时间;使用它来清理资源,当然,但不是为了程序正确性(如果在无效会话上调用方法,你会得到 IllegalStateExceptions ).如果你觉得你真的必须依赖这个,也许可以解释你在做什么,以便其他人可以建议更合适的方法来实现这一目标.

In any case, it seems almost certain that the servlet is doing what you want - seeing that the session is timed out and calling the appropriate method - the only question is whether you're going to see this exactly 60 seconds after the last request or a bit later. I would suggest that in general you shouldn't rely on the exact timings of when this method is called; use it to clear up resources, sure, but not for something like program correctness (you'll get IllegalStateExceptions if calling methods on an invalid session anyway). If you feel you really must rely on this, perhaps explain what you're doing so that others can suggest more suitable ways to achieve this.

这篇关于会话超时时如何调用 sessionDestroyed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆