java 识别会话超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15223841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:54:45  来源:igfitidea点击:

recognizing session timeout

javaservletssession-timeout

提问by YotamB

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

我正在使用 servlet 构建一个 Java 网页棋盘游戏。我需要知道用户在 30 秒内没有接听,我正在使用

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.

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

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

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

Thank you.

谢谢你。

回答by skuntsel

You need to implement the HttpSessionListenerinterface. 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:

您需要实现该HttpSessionListener接口。它在创建或销毁会话时接收通知事件。特别是,它的方法sessionDestroyed(HttpSessionEvent se)在会话被销毁时被调用,这发生在超时期限结束/会话失效之后。您可以通过HttpSessionEvent#getSession()调用获取存储在会话中的信息,然后对会话进行任何必要的安排。另外,请务必在以下位置注册您的会话侦听器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);

回答by YotamB

I ended up using the HttpSessionListener and refreshing in an interval larger then setMaxInactiveInterval.

我最终使用了 HttpSessionListener 并以比 setMaxInactiveInterval 大的间隔刷新。

So if the used did nothing for 30 Sec in the next refresh after 40 Sec I get to sessionDestroyed().

因此,如果在 40 秒后的下一次刷新中使用了 30 秒,我将进入 sessionDestroyed()。

Also important that you need to create new ServletContext to get to the ServletContext.

同样重要的是,您需要创建新的 ServletContext 以访问 ServletContext。

ServletContext servletContext=se.getSession().getServletContext();

Thank you!

谢谢!

回答by kensei62

An alternative to guessing based on the idle interval is to set an attribute in the session when the logout is triggered by the user. For example, if you can put something like the following in the method that handles user triggered logouts:

基于空闲间隔进行猜测的另一种方法是在用户触发注销时在会话中设置一个属性。例如,如果您可以在处理用户触发的注销的方法中放置类似以下内容:

httpServletRequest.getSession().setAttribute("logout", true);
// invalidate the principal
httpServletRequest.logout();
// invalidate the session
httpServletRequest.getSession().invalidate();

then you can have the following in your HttpSessionListener class:

那么你可以在你的 HttpSessionListener 类中有以下内容:

@Override
public void sessionDestroyed(HttpSessionEvent event) {
    HttpSession session = event.getSession();
    if (session.getAttribute("logout") == null) {
        // it's a timeout
    }
}