Enable GZIP compression in tomcat

What is GZIP?

It is a compression format created by Jean-Loup Gailly and Mark Adler. Version 0.1 was first publicly released on October 31, 1992. GZIP is based on the DEFLATE algorithm, which is a combination of LZ77 and Huffman coding. DEFLATE was intended as a replacement for LZW and other patent-encumbered data compression algorithms which, at the time, limited the usability of compress and other popular archivers.

How does it helps in improving performance of my web page?

Modern browser like Internet Explorer, Firefox, Opera etc supports gzip compressed content. These browsers are capable of uncompressing gzip data into plain text. Servers like Apache, Tomcat, JBoss etc supports gzip compression too. Hence if gzip is enabled in such servers, the response if first compressed and then send to client. Hence this increase performance by many folds.

How to enable GZIP in my tomcat server?

Here is how to enable GZIP compression on Tomcat: edit file /conf/server.xml and add to the HTTP Connector configuration something like this:
compression="on" compressionMinSize="2048" noCompressionUserAgents="gozilla, traviata" compressableMimeType="text/html,text/xml"
Code language: HTML, XML (xml)
An example of a HTTP Connector configuration with compression enabled could be:
<Connector port="8080" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" compression="on" compressionMinSize="2048" noCompressionUserAgents="gozilla, traviata" compressableMimeType="text/html,text/xml"/>
Code language: HTML, XML (xml)
Check out following header information before enabling GZIP. And after enabling GZIP look at the size of the webpage and the time taken for loading:

View Comments

  • He dude, why do you wanna do compression on tomcat/coyote..
    most of the people ,when they go production, put the tomcat behind an apache or lighttpd or nginix , as a cluster
    why not the actual webserver do the compression..tomcat compresses in java....while these web servers do it natively..thus much faster and production suitable.

    from somewhere on internet:
    this could certainly be useful if you are running Tomcat standalone, and using apache compression is simply unavailable.
    http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=56&t=003260

    anyways gud job.
    cheers
    Aditya

  • Thanks Aditiya for the comment...
    Definitely Apache server can be used that can do this.. But sometime back, I wanted to use only Tomcat and hence this code.

    anyways. thanks for the info :)
    Cheers,
    Viral

  • Can anyone help me?????

    I have modified the server.xml conf file to set the \"compression\" properties as specified above in the connector tag.

    I have tested this in FireFox in which I am able to notice the difference in the size. But When tested in the IE7 there is no difference in the size of the page. Can anyone explain me how to figure out the issue with IE7.

  • Thanks Viral for your help.

    I had installed the debugger and checked for the output and not found the Content-Encoding, but seen Transfer-Encoding for which the value is \"chunked\". In IE I noticed the size is 6030 bytes but in FF it is 1519 bytes.

    I turned off the Compression and verified the output. The size is same in IE and FF (6030 bytes). Now the Transfer-Encoding header was vanished. Can you kindly let me know why the gzip is not working in IE and how to initialize the compression in IE.

    Thanks in advance.

  • I turned on tomcat compression, but I require a java client to get the result that is returned and decompress the data in the client. I have tried to use GZipInputStream, but I get IOException: GZip trailer. Can any one help? Ignoring this block is not an option.

  • Hi Dear,
    I have enabled this option but after that web page is opening without compress mode, please help...?

  • @Lovelesh - Make sure you also add the mime types for javascript and css. My tomcat wasn't compressing those until I added these mimetypes:

    text/html,text/xml,text/plain,text/css,text/javascript

  • Hi Ashish, thanks for reply.
    I have added as given your reply but it is still working with unzip mode: I am adding here code please review:

    ""
    ..please help

  • Hi all,
    Now i would like to compress the javascript and css files in tomcat 6.0.20 (windows vista),
    1. Moidified server.xml like this:

    2. then test it in firefox 3.5, but i seem doesn't work, why?
    Can you help me?

    Thanks in advance!

Recent Posts

  • Java

Java URL Encoder/Decoder Example

Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…

4 years ago
  • General

How to Show Multiple Examples in OpenAPI Spec

Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…

4 years ago
  • General

How to Run Local WordPress using Docker

Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…

4 years ago
  • Java

Create and Validate JWT Token in Java using JJWT

1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…

4 years ago
  • Spring Boot

Spring Boot GraphQL Subscription Realtime API

GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…

4 years ago
  • Spring Boot

Spring Boot DynamoDB Integration Test using Testcontainers

1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…

5 years ago