【问题标题】:Session timeout not happening会话超时没有发生
【发布时间】:2014-04-18 07:10:06
【问题描述】:

识别会话超时并重定向它的步骤是什么,我尝试在 web.xml 中将会话超时设置为 1 分钟,这是否会使会话超时?

用于在过滤器中重定向:

if (request.getRequestedSessionId() != null   && !request.isRequestedSessionIdValid()) 
        response.sendRedirect(request.getContextPath() + "/login.html");
                    return;
    }
    else {
        response.setHeader("Cache-Control", "no-cache, no-store, must-       revalidate"); // HTTP 1.1.
        response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
        response.setDateHeader("Expires", 0);
        filterChain.doFilter(servletRequest, servletResponse);
    }  

但它没有重定向到登录页面,我在这里缺少什么?

需要进一步澄清的是:web.xml 中的 url-pattern 标记是否应该具有重定向页面地址?

<filter-mapping>
    <filter-name>SessionTimeoutCookieFilter</filter-name>
    <url-pattern>login.html</url-pattern>
</filter-mapping>

请对此提出任何建议....

【问题讨论】:

标签: java session servlets web.xml


【解决方案1】:

如果您使用的是Apache Tomcat,请在您的web.xml 中尝试此操作

         <session-config>
            <session-timeout>30</session-timeout>
        </session-config>

【讨论】:

  • 是的,我们只使用 Apache Tomcat。已将超时设置为 1 分钟,但它不起作用。 1
【解决方案2】:

这很简单。
将以下代码添加到您的 SessionTimeoutCookieFilter

    HttpSession session = request.getSession(false);
        if (null == session) {
            response.sendRedirect("index.jsp");
        }

还可以更改您的 url 模式,就像我使用过的一样:-

<filter>
<filter-name>SessionFilter</filter-name>
 <filter-class>
    net.SessionFilter
</filter-class>
<init-param>
    <param-name>avoid-urls</param-name>
    <param-value>index.jsp</param-value>
</init-param>
</filter>
<filter-mapping>
 <filter-name>SessionFilter</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>

【讨论】:

    【解决方案3】:
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>
    

    或者您可以使用 Java 代码:

    session.setMaxInactiveInterval(15 * 60); //15 minutes
    

    顺便说一下,HttpSession#setMaxInactiveInterval() 在这里没有太大变化。它与web.xml 中的&lt;session-timeout&gt; 完全相同,唯一的区别是您可以在runtime 期间以编程方式更改/设置它。顺便说一句,更改只会影响当前会话实例,而不是全局影响(否则它会是 static 方法)。

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

    您可以使用-1 会话永不过期。因为您不知道线程完成需要多长时间。

    【讨论】:

    • 感谢您的输入,已在 session-config 中设置了超时。但是在会话超时后重定向没有发生,这就是我的位置。有什么想法吗?
    • @user3485046 : 使用过滤器,我没用过这个但是看代码jguru.com/forums/view.jsp?EID=1248692
    • 我也尝试过这些方法......但我想知道,当会话超时发生时,我们需要让会话为空还是它会自动变为空?那么那个会话的用户 ID 会变成 null 吗?
    • 是的,它变成了空值。所以我们不用手动去做,它会自动invalidate()到会话。
    猜你喜欢
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2018-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多