Introduction To Tek271 GzipServletFilter

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

By Abdul Habra
Email ahabra@yahoo.com
URL www.tek271.com
Version  1.00
Date 2007.11.28

Downloads
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 :
    <filter>
      <filter-name>gzipFilter</filter-name>
      <filter-class>com.tek271.gzipServletFilter.GzipServletFilter</filter-class>
      <init-param>   <!-- optional: log filter usage to console -->
        <param-name>isLog</param-name>
        <param-value>true</param-value>
      </init-param>
    </filter>

    <filter-mapping>  <!-- compress the response for any html request -->
      <filter-name>
    gzipFilter</filter-name>
      <url-pattern>*.html</url-pattern>
    </filter-mapping>

    <filter-mapping>  <!-- compress the response for any jsp request -->
      <filter-name>gzipFilter</filter-name>
      <url-pattern>*.jsp</url-pattern>
    </filter-mapping>
  5. 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.
Changes

Version 1.00, 2007.11.28
First public release

Comments




ċ
tek271.gzipServletFilter.2007.11.28.zip
(62k)
Abdul Habra,
Mar 28, 2011, 3:17 PM
Comments