Java GZIP Compression Filter Example

Java GZIP Compression Filter Example explains about implementing a GZIP compression filter for optimizing web application performance

How To Compress HTML Content Using Gzip Servlet Filter?

GZIP is compression algorithm used for compression text files such as HTML, JavaScript, CSS etc, Which will reduce the data (bandwidth) sent to browser

Here we are discussing about implementing Gzip Compression Filter using Java Servlet Filter, if you don't know about Servlet Filter, you can follow servlet filter Tutorial

By implementing GZIP compression, will dramatically improve web application performance and reduce download time.

For availing GZIP compression, your browser must support GZIP compression, you need to set Accept-Encoding request header

You need to download

  1. Tomcat 7
  2. pjl-comp-filter.jar

Following jar must be in classpath

  1. pjl-comp-filter.jar
PJL Compressing Filter

For implementing GZIP Compression Filter, I am here using, PJL Compressing Filter a free available filter.

Ehcache have another Gzip Compression Filter, you can checkout the code below

Just add the pjl-comp-filter-XX.jar file containing CompressingFilter to your web application's WEB-INF/lib directory.

Project Strcuture

Java GZIP Compression Filter

web.xml (GZIP compression filter configuration)

Add the following entries to your web.xml deployment descriptor:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
	id="WebApp_ID" version="3.0">
	<display-name>GZIP Compression Filter</display-name>

Just create a page with some dummy data for testing

Ie; Test.html (In this example)

You can see the file size and transfer size difference in between following two images (Here using Google Page speed for comparison)

Before Compression

Java GZIP Compression Filter

After Compression

Java GZIP Compression Filter


1 Responses to "Java GZIP Compression Filter Example"
  1. sathish 2013-10-10 10:52:14.0

Your email address will not be published. Required fields are marked *