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
Related Q & A:
- How To Make A Web Site?Best solution by Yahoo! Answers
- How Much Does A Web Designer Charge?Best solution by Yahoo! Answers
- How to make a web crawler?Best solution by Stack Overflow
- How to call .aspx page from a web web service(service.svc?Best solution by Stack Overflow
- How do I change a humidifier filter attached to my furnace?Best solution by Ask.com old
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.