Introduction To Tek271 GzipServletFilter

A Java open source servlet filter to compress HTTP responses using GZip.


This software is open source, free, and uses LGPL license.

tek271.gzipServletFilter.2007.11.28.zip: Source, JAR, JavaDocs.

Java Docs.

1. Background

Modern browsers can receive HTTP Response data in a compressed format (GZip). This compression can decrease the bandwidth requirement of web pages, and speeds up the display of these pages at the client. If you are running a Java web application you can use the Tek271 GzipServletFilter to transparently compress the data you send to the client. This compression will occur without any modifications or changes to your application's code.

Tek271 GzipServletFilter works as a Servlet Filter you install with your application. It will detect if the browser supports GZip compression, and if it does, the filter will compress the response data and sends it back to the client.

2. Required Setup

    1. You need JDK 5 or higher.

    2. Download Tek271 GzipServletFilter zip file. Unzip it. In the build directory, find tek271.gzipServletFilter.jar file.

    3. Make sure to put tek271.gzipServletFilter.jar in your application's class path. Usually this could be in your application's WEB-INF/libdirectory.

    4. Add the following to your application's web.xml :

    5. <filter>

    6. <filter-name>gzipFilter</filter-name>

    7. <filter-class>com.tek271.gzipServletFilter.GzipServletFilter</filter-class>

    8. <init-param> <!-- optional: log filter usage to console -->

    9. <param-name>isLog</param-name>

    10. <param-value>true</param-value>

    11. </init-param>

    12. </filter>

    13. <filter-mapping> <!-- compress the response for any html request -->

    14. <filter-name>gzipFilter</filter-name>

    15. <url-pattern>*.html</url-pattern>

    16. </filter-mapping>

    17. <filter-mapping> <!-- compress the response for any jsp request -->

    18. <filter-name>gzipFilter</filter-name>

    19. <url-pattern>*.jsp</url-pattern>

    20. </filter-mapping>

    21. And that's it.

The setup above will cause all requests for *.html or *.jsp files to have GZip compressed responses. (If the client's browser does not support compression, then the filter will not do any compression).

3. References

    1. Two Servlet Filters Every Web Application Should Have by Jayson Falkner: This article on www.onJava.com shows how to create a compression servlet filter and provides a simple implementation.

    2. Apache Tomcat example CompressionFilter: A compression filter provided as an example with Tomcat.

    3. If you search the internet for "java compression filter" you should find several other implementations.

You may wonder why create another implementation with all the above examples. I had the following reasons:

    1. All the implementations I found where either examples with "examples quality",

    2. or had licensing I did not want to use.


Version 1.00, 2007.11.28

First public release