会话超时问题后的Servlet过滤器

时间:2015-03-19 12:50:23

标签: java session redirect servlet-filters

我有Servlet过滤器会话超时检查:

 @Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) servletRequest;
    HttpServletResponse res = (HttpServletResponse) servletResponse;
    String uri = req.getRequestURI();
    if (req.getUserPrincipal() == null && !uri.endsWith("login.xhtml") && uri.endsWith("xhtml")) {
        res.reset();
        res.setHeader("Location", "/nsi/faces/login.xhtml");
        res.setStatus(HttpServletResponse.SC_FOUND);
        res.setContentType("xhtml");
        //  filterChain.doFilter(servletRequest, servletResponse);
        // req.getServletContext().getRequestDispatcher("/faces/login.xhtml").forward(req, res);
    } else {
        filterChain.doFilter(servletRequest, servletResponse);
    }
}

为什么会话超时重定向不起作用? 我尝试使用HttpServletResponse.sendRedirect和HttpServletResponse转发。 附:获取请求重定向,但POST请求entrer在方法中并且什么都不做;

2 个答案:

答案 0 :(得分:0)

在JSF中你可以这样做

FacesContext.getCurrentInstance()。getExternalContext()                     .redirect(" /faces/login.xhtml");

答案 1 :(得分:0)

response.sendRedirect("/faces/login.xhtml");之后,您应该return;

以下过滤器会将对根网址的所有请求重定向到google.nl:

@WebFilter("/*")
public class RedirectFilter implements Filter {

    public void destroy() {}

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
            FilterChain chain) throws IOException, ServletException {
        HttpServletResponse res = (HttpServletResponse) servletResponse;
        res.sendRedirect("https://www.google.nl");
        return;
    }

    public void init(FilterConfig arg0) throws ServletException {}

}