What is a web filter?

How do a web filter in JSF 2?

  • I create this filter : public class LoginFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpSession session = req.getSession(); if (session.getAttribute("authenticated") != null || req.getRequestURI().endsWith("login.xhtml")) { chain.doFilter(request, response); } else { HttpServletResponse res = (HttpServletResponse) response; res.sendRedirect("login.xhtml"); return; } } @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void destroy() { } } This is my structure: And then I add the filter in the web.xml: <filter> <filter-name>LoginFilter</filter-name> <filter-class>filter.LoginFilter</filter-class> </filter> <filter-mapping> <filter-name>LoginFilter</filter-name> <servlet-name>Faces Servlet</servlet-name> </filter-mapping> The filter works as it should but keeps giving me this error: "Was not possible find or provider the resource, login" And after that my richfaces doesn't works anymore. How can I solve that ? Or create a web filter correctly ?

  • Answer:

    Any path-relative URL (i.e. URLs which do not start with /) which you pass to sendRedirect() will be relative to the current request URI. I understand that the login page is at http://localhost:8080/contextname/login.xhtml. So, if you for example access http://localhost:8080/contextname/pages/user/some.xhtml, then this redirect call will actually point to http://localhost:8080/contextname/pages/user/login.xhtml, which I think don't exist. Look at the URL in your browser address bar once again. To fix this problem, rather redirect to a domain-relative URL instead, i.e. start the URL with /. res.sendRedirect(req.getContextPath() + "/login.xhtml");

Valter Henrique at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.