Improve Performance by caching and compression

Web page designs are becoming innovative with rich interface which involve extra code such as java scripts, css, and images etc. Most of the end-user response time tied-up in downloading these components.Optimizations of number of http requests and response size are the key parameters to improve the web application performance.

This article explains about caching filter and compression filter that are suitable for use with any web application to optimize number of request and response size.

Compressing Content Gzip Servlet Filter

HTTP Compression is a way to compress content transferred from servers to browsers which reduce the http response size. This standards-based method of delivering compressed content is built into HTTP/1.1, and all modern Web browsers support the HTTP/1.1 protocol, i.e. they can decode compressed files automatically at the client-side browser. The smaller the size of your information, the faster it can all be sent. Therefore, if you compress the content your web application, it will be displayed on the user’s screen faster.

Source Code:Download link OpenWebOptimizer for Gzip Servlet filter source code with sample code and usage document. 

Gzip is the most popular and effective compression method at this time. It was developed by the GNU project and standardized by RFC 1952. Gzipping generally reduces the response size by about 70%.

For adding the Gzip filter just adds below code into web.xml including attached Gzip filters


HTTP Servlet Caching:

Caching reduces number of HTTP request which makes web page faster. Web application generate browser content and in many scenario content won’t change between different requests therefore if you can cache the response ,you can reuse again without HTTP requests which improve the web application performance. We can achieve caching by just writing simple caching filter

Source Code: Download link OpenWebOptimizer for Caching Servlet filter source code with sample code and usage document.

Caching filter means a servlet that will intercept all requests and cache it and have a valid cached copy. The filter will immediately respond to the requests by sending a copy of cached contents. However, if no cache exists, the filter will pass the request on to its intended endpoint, and the response will be generated and cached for future request.

For adding the caching filter just add below code into web.xml

<!-- Change this to add the expiry time for re-validating the files -->


Caching and Compression filters optimize HTTP request call, contents size and contents generation. Caching and Compression are most important task in terms of web application performance.  We can use attached project which is free and open source to use in your application.

Reference: Improve Performance by caching and compression from our JCG partner Nitin Kumar at the Tech My Talk blog.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Nitin Kumar

Nitin Kumar works as a Software Architect , predominately focus on Agile, TDD, Service Oriented Architecture, Grails and JEE. Besides working in software development, He writes technical articles and watches and studies new technologies
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
8 years ago

couple of questions:

how is servlet caching different from setting the HTTP cache headers? in the latter case the round trips can be avoided if the browser ends up serving the request from its own cache.

Back to top button