Reputation: 35282
I have a Filter Servlet that filters request from a Servlet. I do not want to do something on a request for a Image, so I have for example this code:
if (baseURL.endsWith("png"))
{
chain.doFilter(servletrequest, servletresponse);
}
Which on the doFilter
method of of the filter, I don't do anything and just call chain.doFilter
, I expected that the response image will be sent properly in the client however the Content-Type
that comes back is "application/octet-stream"
instead of "image/png"
Any idea why this happens?
Upvotes: 2
Views: 1561
Reputation: 1108782
I have a Filter Servlet that filters request from a Servlet. I do not want to do something on a request for a Image
The FilterChain#doFilter()
does not prevent that the request ends up in your servlet if its URL pattern also matches the request URL. You seem to be thinking that this is the case. This is thus not correct.
If you do not want that image requests end up in your servlet, then you have to map the servlet on a more specific URL pattern. For example, /app/*
instead of /*
. Then you can filter the requests as follows:
if (httpServletRequest.getRequestURI().endsWith(".png")) {
chain.doFilter(request, response);
}
else {
request.getRequestDispatcher("/app" + httpServletRequest.getRequestURI()).forward(request, response);
}
The URL pattern of /*
should not be used on servlets, but on filters only.
Further, the content type is by default already automatically determined based on the file extension. I assume that you really have a some.png
resource, not a somepng
resource as your initial endsWith()
argument value would also match.
Upvotes: 2
Reputation: 9505
You should escape from the filter chain and set the content type:
if(baseURL.endsWith("png")) {
response.setContentType("image/png");
} else {
chain.doFilter(request, response);
}
Upvotes: 1
Reputation: 240898
put return;
statement
if (baseURL.endsWith("png"))
{
chain.doFilter(servletrequest, servletresponse);
return;
}
Upvotes: 1