为什么我在会话超时后无法访问 servlet? [英] Why I can't access the servlet after the session time out?
问题描述
我有一个 JSP、Servlet、Hibernate 应用程序.在这个应用程序中,我有一个非常奇怪的问题.也就是说,如果会话已过期(换句话说超时")并且当用户单击链接时页面将被重定向到索引页面,但之后用户无法登录并访问最后一个他点击的链接.我将在下面一步一步地描述它.
I have a JSP, Servlet, Hibernate application. In this application I have a very weird problem. That is, if the session got expired (in other words "time out") and when the user click on a link the page will be redirected to the index page, but after that the user is not able to log in and access the last link he clicked. I will describe it step by step in below.
- 用户登录应用程序.会话已创建.
- 他访问路径/Passport
- 用户现在空闲,会话已过期.
- 用户返回并点击链接访问 /Visa .由于会话现在空闲,用户将被重定向到索引页面.
- 用户登录.
点击链接访问/Visa(从任何有链接的地方).链接是链接到其路径的地方,如
- User log into the application. Session get created.
- He access the path /Passport
- User is now idle, session get expired.
- User come back and click on link to access /Visa . Since the session is now idle, user will be redirected to index page.
- User log in.
Click on the link to access /Visa (from anywhere where the link is available) . The link is an where it links to its path like
Visa?idEmployee=1
Visa?idEmployee=1
问题来了.用户被重定向回索引页面.
Now the problem. User is redirected back to index page.
我有Filter
来监控会话是否为null
以及所需的会话属性是否不是null
.如果请求不满足上述 2 个条件,请求将被发送回索引.
I have Filter
to monitor whether the session is null
and whether the required session attributes are not null
. If the request do not fulfill the mentioned 2 conditions, the request will be sent back to the index.
过滤器代码如下.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
*
* @author user
*/
public class AuthenticationFilter_Level1 implements Filter
{
@Override
public void init(FilterConfig filterConfig) throws ServletException
{
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
{
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)res;
HttpSession session = request.getSession(false);
Integer attribute = null;
if(session!=null && session.getAttribute("idSubUser")!=null)
{
chain.doFilter(req, res);
}
else
{
//response.sendRedirect("index.html");
RequestDispatcher dispatch = request.getRequestDispatcher("index.html");
dispatch.forward(req, res);
}
}
@Override
public void destroy()
{
}
}
在 web.xml 中,我添加了从 servlet 到 servlet 的过滤器,如下所示.
In web.xml, I have added the filter from servlet to servlet, like below.
<filter-mapping>
<filter-name>AuthenticationFilter_Level1</filter-name>
<url-pattern>/RegSrvlt</url-pattern>
<url-pattern>/AdminPopulateSrvlt</url-pattern>
<url-pattern>/AgentPopulate</url-pattern>
......
过滤器会话超时配置如下.
Filter session timeout is configured as below.
<session-config>
<session-timeout>
1
</session-timeout>
</session-config>
那么,这里发生了什么?
So, what is happening here?
更新
当上面的错误发生时,URL实际上看起来像http://localhost:8080/xxx/Visa?idEmployee=1
,即使它被重定向了!
When the above error happens, the URL actually looks like http://localhost:8080/xxx/Visa?idEmployee=1
even though it is redirected!
更新
我发现这与过滤器无关.那么什么可以做到这一点?
I found this has no connection with the filter. Then what can make this?
推荐答案
else
{
if (session != null) {
session.invalidate();
}
...
并检查创建会话的位置(即公共 JSP/servlet).
And check the where sessions are created (i.e. public JSPs/servlets).
这篇关于为什么我在会话超时后无法访问 servlet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!