Reputation: 585
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>
Upvotes: 3
Views: 1876
Reputation: 15446
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:
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 (!currentURL.equals("/maintenance.jsp")
&& maintenanceMode.getMaintenanceMode())
{
String urlNew = currentURL.concat("maintenance.jsp");
response.sendRedirect(urlNew);
}
filterChain.doFilter(servletRequest, servletResponse);
}
I did not understand why are you doing String urlNew = currentURL.concat("maintenance.jsp");
for every url. Do you have maintenance.jsp
page relative to every url. I think you should be having a single /maintenance.jsp
page. And in such case it is have to redirect always to response.sendRedirect(/maintenance.jsp)
irrespective of page you are accessing.
Upvotes: 2
Reputation: 7899
<filter-mapping>
<filter-name>maintenanceFilter</filter-name>
<url-pattern>/*</url-pattern>
In filter mapping your all requests (they are new requests as they are being redirected )are eligible for this filter
this is the reason why your filter is being called on every request for infinite times .
One way is you can call this filter for specific url-pattern
Upvotes: 0