识别会话超时 [英] recognizing session timeout

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

问题描述

我正在使用servlet构建一个java web棋盘游戏。
我需要知道用户何时没有应答30秒,我正在使用

I am building a java web board game using servlets. I need to know when a user is not answering for 30 secundes, I am using

session.setMaxInactiveInterval(30);

但是一旦时间结束我需要在服务器端知道,所以我可以完全使用这个播放器。

But I need to know on the server side once the time ended so I can make this player quite.

现在一旦玩家返回并尝试做某事他就会得到超时,我可以在服务器上看到。

As it is now once the player return and try to do something he will get the timeout and I can see on on the server.

一旦会话超时,我怎么能在servlet中知道?!

How can I know in the servlet once a session has timeout?!

谢谢。

推荐答案

您需要实现 HttpSessionListener 界面。它在创建或销毁会话时接收通知事件。特别是,它的方法 sessionDestroyed(HttpSessionEvent se)在会话被销毁时被调用,这在超时期限结束/会话失效后发生。您可以通过 HttpSessionEvent#getSession()调用获取存储在会话中的信息,然后执行会话所需的任何安排。此外,请务必在 web.xml中注册会话侦听器

You need to implement the HttpSessionListener interface. It receives notification events when session is created, or destroyed. In particular, its method sessionDestroyed(HttpSessionEvent se) gets called when the session is destroyed, which happens after timeout period has finished / session was invalidated. You can get the information stored in the session via HttpSessionEvent#getSession() call, and later do any arrangements that are necessary with the session. Also, be sure to register your session listener in web.xml:

<listener>
    <listener-class>FQN of your sessin listener implementation</listener-class>
</listener>

如果您最终要区分失效和会话超时,可以在监听器中使用以下行:

If you ultimately want to distinguish between invalidation and session timeout you could use the following line in your listener:

long now = new java.util.Date().getTime();
boolean timeout = (now - session.getLastAccessedTime()) >= ((long)session.getMaxInactiveInterval() * 1000L);

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

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