【问题标题】:How to check the HttpSession exists in Filter如何检查过滤器中是否存在 HttpSession
【发布时间】:2015-08-04 13:36:49
【问题描述】:

我有一个 javax.servlet.Filter,如果 HttpSession 是新的,我想在其中重定向到登录页面,如果 HttpSession 过期,我想重定向到注销页面,如果存在 HttpSession,我想重定向到其他页面。

The login and logout pages are external pages. My sample method is given below

    @Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
        FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) arg0;
    HttpServletResponse response = (HttpServletResponse) arg1;

    if (request.getSession(false) == null) { 
        response.sendRedirect("login page");
    } else if (request.getRequestedSessionId() != null && !request.isRequestedSessionIdValid()) {
        response.sendRedirect("logout page");
    } else {
        chain.doFilter(request, response);
    }    

}

问题是会话过期后,注销页面不呈现。如何让它发挥作用?

【问题讨论】:

  • 如何区分是新会话还是过期会话?

标签: java web httpsession spring-session


【解决方案1】:

如果会话已过期,getSession(false) 返回 null,因此您重定向到登录页面。您的第二个 if 应该在第一个之前。

【讨论】:

    【解决方案2】:

    或者你可以试试

    getSession().isNew()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-24
      • 1970-01-01
      • 2021-06-27
      • 1970-01-01
      • 2016-01-15
      • 1970-01-01
      • 2021-01-11
      相关资源
      最近更新 更多