I'm trying to implement Java servlet filter, that modifies html responses.
doFilter
method of my filter class looks like that:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (filterConfig == null) {
return;
}
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
String contentType = res.getContentType();
if (contentType != null && contentType.contains("text/html")) {
chain.doFilter(req, res);
// do some modification
}
}
For every response I'm trying to figure it out if it's an HTML. If that's the case, I do some modification, but I have the following problem: When requesting jsf file, res.getContentType()
returns null
(res.getHeader("Content-Type")
also returns null
). In my browser's developer tools I can see that 'Content-Type' header has value 'text/html; charset=UTF-8', but why does res.getContentType()
return null
in that case?
Is there any other way to detect HTML response in filter?
EDIT I've added chain.doFilter(req, res)
invocation in if clause.