2015-03-19 116 views
0

我有蒙山會話超時檢查Servlet過濾器: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轉發。 P.s.獲取請求重定向,但POST請求進入方法並且什麼都不做;

回答

0

在JSF中,你可以做這樣

FacesContext.getCurrentInstance()getExternalContext() .redirect( 「/面/ login.xhtml」)。

+0

這種方法拋出顯示java.lang.NullPointerException – 2015-03-19 13:02:34

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 {} 

} 
+0

「迴歸」中的if-else塊什麼都不做)) – 2015-03-20 06:44:18

+0

我加入一個將所有電話重定向到www.google.nl的示例過濾器。 – 2015-03-20 08:04:04

+0

它適用於GET請求,但不適用於post =( – 2015-03-20 08:44:30