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
http://svn.terracotta.org/svn/ehcache/trunk/web/web/src/main/java/net/sf/ehcache/constructs/web/filter/GzipFilter.java

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="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>GZIP Compression Filter</display-name>
	<filter>
		<filter-name>CompressingFilter</filter-name>
		<filter-class>com.planetj.servlet.filter.compression.CompressingFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>CompressingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>
Output

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 *