Enable GZIP compression in tomcat
- By Viral Patel on November 28, 2008
- Featured, How-To
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
compression="on" compressionMinSize="2048" noCompressionUserAgents="gozilla, traviata" compressableMimeType="text/html,text/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"/>
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:

Get our Articles via Email. Enter your email address.




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.
Hi Sateesh,
Use some plugins for IE to check HTTP Header and see if the CONTENT-ENCODING is GZIP. You can use http://www.debugbar.com/download.php to check this.
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!
1. Moidified server.xml like this:
..Connector port=”8080″ protocol=”HTTP/1.1″
connectionTimeout=”20000″
redirectPort=”8443″
compression=”on”
compressionMinSize=”2048″
noCompressionUserAgents=”gozilla, traviata”
compressableMimeType=”text/html,text/xml,text/plain,text/javascript,text/css”
…
Thanks guys,
This function is working fine!
It’s my problem: I should clear firefox recent history first and then test it.
Best regards!
Hi all,
Really a nice and useful article.
But I am also facing the IE problem that the response is not compressed. I installed the debug bar and checked. No ‘CONTENT-ENCODING : GZIP’ in the response. Also the response size is the same with/without the compression.
Any idea?
Hi Viral,
i got the same problem like Muiz, the same configuration and Tomcat doesn’t do gzip. any Ideas??
@rasha, @choesang: Generally the problem that you are facing is due to browser cache issue. Clear your browser cache and try again.
Simply, IE is not a browser..
Where can I find info on how a client can UNCOMPRESS this data? All these articles/topics assume the server is hit from a browser. The browser handles uncompressing the data automatically. I’m not hitting my server from a browser. I’m using another client program.
Thanks,
Worked like a charm!
..Connector port=”8080″ protocol=”HTTP/1.1″
connectionTimeout=”20000″
redirectPort=”8443″
compression=”on”
compressionMinSize=”2048″
noCompressionUserAgents=”gozilla, traviata”
compressableMimeType=”text/html,text/xml,text/plain,text/javascript,text/css”
Hi,
Thanks in advance.
I am using WAS5.1 and J2EE 1.4.
I want to use GZip compression in my Struts application.
I am able to use this compression for .jsp and .html file.
Once I configure GZip compression for (/*) patten, it gives below exception.
I am able to used GZip compression for struts application in Tomcat environment, but it does not work for Struts action in WAS5.1.
==================================
[12/3/09 14:32:00:216 IST] 5a8b8c0a WebGroup E SRVE0026E: [Servlet Error]-[]: java.lang.ClassCastException
at com.ibm.ws.cache.servlet.CacheProxyResponse.isCommitted(CacheProxyResponse.java(Compiled Code))
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java(Compiled Code))
at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1069)
at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:455)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:279)
==================================
Let me know if you need some more details.