Is it possible to apply different cache-control in the HTTP header response for specific files in a web app running on Tomcat? I’m looking to do so from the web app without having to change Tomcat’s configuration since those settings change with each deployment.
For instance, here’s an example of what I want:
- Never Cache: /, /index.jsp (I understand that it’s possible to define caching in the header from the JSP or servlet class).
- Cache: /js/, /img/, /css/* (cache these files for 3 months)
I appreciate your answers.
Thanks
This is a definitely a job for ServletFilters as they allow you to intercept requests.
This way you can easily set the correct HTTP Headers for caching
I started writing a solution to this a few minutes ago using two ServletFilters.
One filter to cache the resources and one not to cache them.
However, I stumbled upon cache-filter and it already does the job for you…
It looks like you need to point your Never cache resources to the
NoCacheFilter
And the cachable resources to the
CacheFilter