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.


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 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...

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

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

Tanika Co Valda said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java EE Online Training from India . Nowadays Java has tons of job opportunities on various vertical industry.

Sphinax Info Systems said...

I like Your Post.......Thank you For Your Sharing

best web development company in chennai
hotel billing software in chennai
hrms software in chennai
payroll software in chennai
sap r3 software in chennai

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

shanjames said...

It’s really great information for becoming a better Blogger. Keep sharing, Thanks...
Data Science Training In Hyderabad