That’s my filter:
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws java.io.IOException, javax.servlet.ServletException
{
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
String currentURL = request.getRequestURI();
MaintenanceService maintenanceMode = new MaintenanceService();
if (maintenanceMode.getMaintenanceMode())
{
String urlNew = currentURL.concat("maintenance.jsp");
response.sendRedirect(urlNew);
}
filterChain.doFilter(servletRequest, servletResponse);
}
“under” maintenanceMode.getMaintenanceMode(), I have variable getter:
boolean maintenanceMode = Boolean.getBoolean("maintenance");
With forward (server-side redirect) it works fine, when I’m trying client-side redirect:
response.sendRedirect(urlNew);
I got an infinite occurrences maintenance.jsp concatenating:
http://localhost:8080/maintenance.jspmaintenance.jspmaintenance.jspmaintenance.jspmaintenance.jsp
why it’s not redirecting onto one occurrence, like with server-side redirect:
http://localhost:8080/maintenance.jsp
Web.xml filter mapping:
<filter-mapping>
<filter-name>maintenanceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
This is happening because you are redirecting for all URLS including
maintenance.jsp. And it is resulting in an infinite redirection.Modified the redirection condition to redirect only if the current url is not
maintenance.jsp.Below is how the code looks after my change:
I did not understand why are you doing
String urlNew = currentURL.concat("maintenance.jsp");
for every url. Do you havemaintenance.jsppage relative to every url. I think you should be having a single/maintenance.jsppage. And in such case it is have to redirect always toresponse.sendRedirect(/maintenance.jsp)irrespective of page you are accessing.