I want to set the Expires
header for all image/*
and text/css
. I'm doing this in a Filter
. However:
chain.doFilter(..)
the Content-type is not yet "realized"chain.doFilter(..)
the Content-type is set, but so is content-length, which forbids adding new headers (at least in Tomcat implementation)I can use the extensions of the requested resource, but since some of the css files are generated by richfaces by taking them from inside jar-files, the name of the file isn't x.css
, but is /xx/yy/zz.xcss/DATB/...
.
So, is there a way to get the Content-type before the response is committed.
Select the web site where you want to add the custom HTTP response header. In the web site pane, double-click HTTP Response Headers in the IIS section. In the actions pane, select Add. In the Name box, type the custom HTTP header name.
You can set response headers, you can add response headers And you can wonder what the difference is. But think about it for a second, then do this exercise. Draw a line from the HttpResponse method to the method's behavior.
Yes, implement HttpServletResponseWrapper
and override setContentType()
.
class AddExpiresHeader extends HttpServletResponseWrapper {
private static final long ONE_WEEK_IN_MILLIS = 604800000L;
public AddExpiresHeader(HttpServletResponse response) {
super(response);
}
public void setContentType(String type) {
if (type.startsWith("text") || type.startsWith("image")) {
super.setDateHeader("Expires", System.currentTimeMillis() + ONE_WEEK_IN_MILLIS);
}
super.setContentType(type);
}
}
and use it as follows:
chain.doFilter(request, new AddExpiresHeader((HttpServletResponse) response));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With