在 web.xml 中设置的超时在 java 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13275851/
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 12:05:22  来源:igfitidea点击:

Timeout set in the web.xml is not working in java

javajsflistenersession-timeout

提问by SRy

I am trying to set my application timeout on Tomcat 7 app server.First I am testing with my timeout as one minute in web.xml as

我正在尝试在 Tomcat 7 应用程序服务器上设置我的应用程序超时。首先我在 web.xml 中测试我的超时时间为一分钟

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

and I am using HttpSessionListenerto make sure my Timeout is working fine.I declared my sessionListener Class in web.xml .

我正在使用HttpSessionListener来确保我的超时工作正常。我在 web.xml 中声明了我的 sessionListener 类。

public class HttpSessionChecker implements HttpSessionListener {

    public void sessionCreated(HttpSessionEvent event) {
        System.out.printf("Session ID %s created at %s%n", event.getSession().getId(), new Date());
    }
    public void sessionDestroyed(HttpSessionEvent event) {
        System.out.printf("Session ID %s destroyed at %s%n", event.getSession().getId(), new Date());
    }
}

and In web.xml

并在 web.xml

<listener>
    <listener-class>com.test.util.HttpSessionChecker</listener-class>
</listener>

But when start my server and launch my application I see session is initiated on the Login page only .

但是当启动我的服务器并启动我的应用程序时,我看到会话仅在登录页面上启动。

Session ID 934073ED5E9933158995EE5EB680D3F7 created at Wed Nov 07 09:39:13 PST 2012

and when I stay Idle for more than a minute or some times more than five minutes cause Tomcat don't fire Timeout immediately in my application and click on something in my application Session is not Expired.Still I am able to navigate another page.But When I am stay Idle on the login page or logged-out I see the session is destroyed. But when once Login is done and when I am inside the application session timeout is not happening. I see

当我保持空闲超过一分钟或超过五分钟时,导致 Tomcat 不会立即在我的应用程序中触发 Timeout 并单击我的应用程序中的某些内容会话未过期。仍然可以导航另一个页面。但是当我在登录页面上保持空闲状态或注销时,我看到会话被破坏了。但是,一旦登录完成并且当我在应用程序中时会话超时不会发生。我明白

Session ID 934073ED5E9933158995EE5EB680D3F7 destroyed at Wed Nov 07 09:42:13 PST 2012

What am I doing wrong? And even after I mentioned <session-timeout>in web.xmldo I need code session.invalidatesomewhere in Code? How Can I redirect to login.xhtmlin session destroyed in JSF?

我究竟做错了什么?而且即使我提到<session-timeout>web.xml,我需要的代码session.invalidate在代码的地方?如何重定向到login.xhtmlJSF 中销毁的会话?

回答by Marcus Blackhall

Your doing nothing wrong. A background thread will check every x minutes for,when the sessions expire. This is container specific. Ie it is not guaranteed to be expired after the time you specify. I know in tomcat you can easily modify this check interval in the server.xml file.

你没有做错什么。后台线程将每 x 分钟检查一次会话何时到期。这是特定于容器的。即不能保证在您指定的时间后过期。我知道在 tomcat 中你可以很容易地在 server.xml 文件中修改这个检查间隔。