简体   繁体   English

在jsp中会话超时后,如何重定向到登录页面?

[英]how can i redirect to login page after session time out in jsp?

Hi i am developing application using struts and jsp. 嗨,我正在使用struts和jsp开发应用程序。 in jsp we are using Ajax calls , after session timeout, we are redirecting to log in page. 在jsp中,我们使用Ajax调用,会话超时后,我们将重定向到登录页面。 but the problem is the log in page is displaying same div tag.i am checking user in session or not in javascript of jsp but always session have userid value, it never going to be null, if session expires also. 但是问题是登录页面显示相同的div标签。我正在会话中检查用户或不在jsp的javascript中检查用户,但会话始终具有userid值,如果会话也到期,它将永远不会为null。

Two things 两件事情

  1. Configure a Welcome page as Login Page in web.xml 在web.xml中将“欢迎”页面配置为“登录页面”
  2. Create a filter and configure in web.xml , this should be the first filter in web.xml 创建一个过滤器并在web.xml中进行配置,这应该是web.xml中的第一个过滤器
  3. In the filter check if the session is new the user should be guided to the login page , else the request should be processed. 在过滤器中,检查会话是否为新会话,应将用户引导至登录页面,否则应处理请求。

Recently I made a tutorial about this exactly. 最近,我确切地制作了一个教程。 Maybe it can be helpful. 也许会有所帮助。 Is the same solution that abhi proposed but with an example. 与abhi提出的解决方案相同,但带有示例。

http://classfoundexception.blogspot.com.es/2012/04/how-to-secure-struts-13-application.html http://classfoundexception.blogspot.com.es/2012/04/how-to-secure-struts-13-application.html

Every time the new request comes, you should check and validate session at server side. 每次收到新请求时,都应在服务器端检查并验证会话。 Also this timeout is handled by web server.Once time out is occurred, automatically server redirect user to session logout URL . 此超时也由Web服务器处理。一旦发生超时,服务器将自动将用户重定向到session logout URL You may change this configuration in conf files of server. 您可以在服务器的conf文件中更改此配置。

For more info see this 欲了解更多信息请参阅

To handle Session Timeout/Expire for Ajax Call Request and to dispatch it to login page follows these steps. 要处理Ajax呼叫请求的会话超时/过期并将其分派到登录页面,请执行以下步骤。

1) In Your jsp wherever ajax function are written Set a header before your ajax send request. 1)在您的jsp中编写ajax函数的任何位置在ajax发送请求之前设置标头。

req.open("POST", Servlet_PATH, true);
req.setRequestHeader("X-Requested-With", "XMLHttpRequest"); //set header
req.send();

2) In a Filter get the header like this and if session is null send as an Response Error 2)在过滤器中获取像这样的标头,如果会话为空,则作为响应错误发送

    httpRequest.getHeader("X-Requested-With");
    if (session == null) {
`if(httpRequest.getHeader("X-Requested-With")!=null && httpRequest.getHeader("X-Requested-With").equals("XMLHttpRequest")){`
        `logger.info("Ajax Expired...");`
                            `((HttpServletResponse)response).sendError(403);` // Response error set
                            `return;`
                        `}}`

3) In jsp whereever ajax code is written check request.readystate and request.state like this 3)在jsp中,无论编写了ajax代码的地方,都请像这样检查request.readystate和request.state

if (req.readyState==4 && req.status==200)
         {
//your logic
}
else if(req.readyState==4 && req.status==403){

             alert("Your Session is Expired.Please Relogin.");
             window.location.href = "<%=request.getContextPath()%>/jsp/login.jsp";
         }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM