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.

7 comments:

mysql services said...


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

Diya Patel 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



preethi s 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

jhansi joe said...

Thanks for your informative article. Java is most popular programming language used for creating rich enterprise, desktop and web applications. Keep on updating your blog with such informative post. J2EE Training in Chennai | JAVA Training in Chennai| JAVA Course in Chennai

Shalini Mudhalayar 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