2 problems with servlet filter
Hi,
I wrote the following filter and wrapper:
* filters parameters: ContentType and expires.
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (filterConfig == null) {
return;
}
long startTime = System.currentTimeMillis();
HttpServletResponse res = (HttpServletResponse) response;
HttpServletRequest req = (HttpServletRequest) request;
JPServletResponseWrapper responseWrapper = new
JPServletResponseWrapper(res);
chain.doFilter(req, responseWrapper);
String result = responseWrapper.toString();
PrintWriter out = response.getWriter();
int length = result.length() >= 10000 ? 10000 : result.length();
int jpHTTPHeaderTag = result.substring(0, length).indexOf("jp-http-
header");
if (jpHTTPHeaderTag != -1) {
int contentTypeIndex = result.indexOf("Content-type",
jpHTTPHeaderTag);
int contentTypeIndexStart = result.indexOf("#", contentTypeIndex);
int contentTypeIndexEnd = result.indexOf("#", contentTypeIndexStart +
1);
String contentType = result.substring(contentTypeIndexStart + 1,
contentTypeIndexEnd);
res.setContentType(contentType);
out.write(result);
} else {
out.write(result);
}
out.close();
long stopTime = System.currentTimeMillis();
System.out.println("Time to execute request: " + (stopTime -
startTime) + " milliseconds");
}
public void init(FilterConfig config) {
filterConfig = config;
}
public void destroy() {
filterConfig = null;
}
but when condition to set the content type as from the html file
applies,
res.setContentType(contentType) - doesn't do anything!