I am developing a dynamic JSP/Servlet web application. In order to handle the session, I am using a filter which is mapped on /* in web.xml. When I’m opening a page in Firefox, it gives the following Firefox-specific error message:
Firefox has detected that the server is redirecting the request for this address in a way that will never complete
A similar error is shown in Chrome. How is this caused and how can I solve it?
Your filter is redirecting to an URL which is invoking the very same filter with the very same conditions again which in turn thus results in a new redirect, etcetera. Your filter is basically redirecting to itself in an infinite loop. The webbrowser is blocking the infinite loop after ~20 requests to save the enduser from badly designed webapplications.
You need to fix your filter accordingly that it is not performing a redirect when it has already been performed. Let’s assume a basic real world example of a login filter which is mapped on
/*which should be redirecting to the login page when the user is not logged in which is identified byuserattribute in session. You obviously want that the filter should not redirect to the login page if the login page itself is currently been requested.You see, if you want to allow/continue a request, just call
chain.doFilter()instead ofresponse.sendRedirect(). Use redirect only if you want to change a request to a different destination.