Monday, January 23, 2012

Servlet Filter to Set Response Headers

Frequent requirement when developing Java web apps is to set different response headers based of on content type / URL. Last project it was to set cache headers on dynamic URLs so they will never be cached and today I had to do the opposite for static content, where cache headers were set on static content so they will be cached for a very long time by the browsers.

It's basically the same thing but just setting different response header values. So I decided to write a general purpose Servlet filter to achieve this in a few lines.



We configure the filter in web.xml and set the required response header / value pair as init params


Here we are setting the Cache-Control response header and setting the expiry date to 1 year from request time. Now we add the appropriate filter mapping to apply the filter to static content we are interested in.


And we are done. In summary all we are doing here is setting cache headers to all static content like .js, .css etc... so they will be cached by the client browsers for a very long time making the pages load faster and reducing the bandwidth usage / load on the server significantly.

Please note you shouldn't add far future cache expiration dates if you plan to make changes to the static content at any time as the modified content might not be downloaded by client browsers. The strategy we take is to make a new copy of the file when we modify and link to the new file. e.g. style-1.1.css when modified will be made site-1.2.css,




Don' forget to click the +1 button below if this post was helpful.

8 comments:

mysql services said...


oh... thanks you for your share. And now, my blog is beautiful

Unknown said...

This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..
Informatica Training in chennai | QTP Training in Chennai



Unknown said...

Thanks a lot! You made a new blog entry to answer my question; I really appreciate your time and effort.java training in chennai |
java training institutes in chennai

Ishu Sathya said...

Thank you for sharing the excellent post about JAVA programming. you helped me to gain more information on the JAVA language.
Best JAVA Training in Chennai |
Java Courses in Chennai

Anoushka Sakthi said...


Wonderful Blog!!! Your post is very informative about Data Management. Thank you for sharing the article with us.

Hadoop Training Chennai |
Big Data Training

Unknown said...

Testing an application is become essential for any product to get an effective result. Your post help you to gain more info on Selenium Testing

Selenium Training in chennai |
Selenium Training

Prwatech said...

I learned World's Trending Technology from certified experts for free of cost. I Got a job in decent Top MNC Company with handsome 14 LPA salary, I have learned the World's Trending Technology from Python training in pune experts who know advanced concepts which can help to solve any type of Real-time issues in the field of Python. Really worth trying instant approval blog commenting sites

Best web development company said...

Hey there

Thanks for sharing this interesting blog, Keep on updating more like this
Best web development company
Best Software Development company
Mobile app development company