Java 会话超时未发生
识别会话超时和重定向会话的步骤是什么?我在web.xml中将会话超时设置为1分钟,这样是否会使会话超时 用于在筛选器中重定向:Java 会话超时未发生,java,session,servlets,web.xml,Java,Session,Servlets,Web.xml,识别会话超时和重定向会话的步骤是什么?我在web.xml中将会话超时设置为1分钟,这样是否会使会话超时 用于在筛选器中重定向: if (request.getRequestedSessionId() != null && !request.isRequestedSessionIdValid()) response.sendRedirect(request.getContextPath() + "/login.html");
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模式标记是否应该具有重定向页面地址
<filter-mapping>
<filter-name>SessionTimeoutCookieFilter</filter-name>
<url-pattern>login.html</url-pattern>
</filter-mapping>
SessionTimeoutCookieFilter
login.html
如果您正在使用ApacheTomcat,请在您的
web.xml
中尝试这一点
<session-config>
<session-timeout>30</session-timeout>
</session-config>
30
顺便说一句,这里的天气变化不大。它与web.xml
中的
完全相同,唯一的区别是您可以在运行时以编程方式更改/设置它。顺便说一下,更改只影响当前会话实例,而不是全局(否则它将是一个静态方法)
-1
您可以在会话永不过期的情况下使用-1
。因为您不知道线程完成需要多少时间。这非常简单
将以下代码添加到您的会话TimeoutCookieFilter
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>
会话过滤器
net.SessionFilter
避免URL
index.jsp
会话过滤器
/*
这取决于您的应用程序服务器。如果您使用的是jboss[这应该对您有所帮助][1]。[1] :是的,我们只使用ApacheTomcat。已将超时设置为1分钟,但它不工作。1感谢您的输入,我已经在session-config中设置了超时。但是在会话超时后不会发生重定向,我就在这里。。有什么想法吗?@user3485046:look@user3485046:use filter,我没有用过这个,但是look代码我也尝试过这些方法…但是我想知道,当会话超时发生时,我们是否需要将会话设为null,或者它将自动变为null?那个会话的用户ID会变成null吗?是的,它会变成null。因此,我们不必手动执行,它会自动invalidate()
到会话。
<session-config>
<session-timeout>-1</session-timeout>
</session-config>
HttpSession session = request.getSession(false);
if (null == session) {
response.sendRedirect("index.jsp");
}
<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>