Tag Archives: Apache Tomcat Configuration

[repost ] Enabling gzip compression in Tomcat

The Aus-e-Stage project which is part of the much larger AusStage project involves the creation of three new services for the AusStage website. One of those services is the Navigating Networks Service which will provide a web based interface to visualise the networks of artistic collaboration represented in the AusStage database.

This service produces large data files, in

particular the XML files used for graph exports and the JSON data used to construct visualisation in the browser. This morning I started exploring the possibilities for using gzip compression to compress the data before it is sent across the network to the browser. In this way the responsiveness of our services could be increased.

Looking through the Apache Tomcat Configuration documentation I found a list of options provided by the Standard Implementation. Two of these options are:

  • compression – used to enable compression in Tomcat
  • compressableMimeType – used to configure which mime-types will be compressed

These options are specified as part of the connector section of the $CATALINA_BASE/conf/server.xml file. For example the server.xml file on my development system now looks like this:

1 <Connector port="8181" protocol="HTTP/1.1"
2 connectionTimeout="20000"
3 URIEncoding="UTF-8"
4 redirectPort="8443"
5 compression="on"
6 compressableMimeType="text/html,text/xml,text/plain,application/javascript,application/json"/>

What worried me was that this would enable compression for static items on the server and not data that was constructed dynamically by my Java Servlets. In fact the opposite was true, data that was dynamically generated by my code was being compressed and static files, such as JavaScript flies, on the server were not.

I thought this must have something to do with the list of mime-types as all of my Java Servlets output data using an appropriate mime-type. For example when sending JSON back to the client I output the JSON mime-type using code like this:

1 // output json mime type
2 response.setContentType("application/json; charset=UTF-8");

Looking in the $CATALINA_BASE/conf/web.xml revealed the issue.

In the web.xml file the mime-type for files with a .js extension was text/javascript and not application/javascript which my code used. The easiest way to fix it was to update the entry in the server.xml file so that it looked like this:

1 <Connector port="8181" protocol="HTTP/1.1"
2 connectionTimeout="20000"
3 URIEncoding="UTF-8"
4 redirectPort="8443"
5 compression="on"
6 compressableMimeType="text/html,text/xml,text/plain,application/javascript,application/json,text/javascript"/>

Please note: That this has only been tested on my development instance of Tomcat and not used in production. Although I plan to implement it when possible. With this in mind please test these changes in your own environment and in particular read and understand the configuration information available on the Tomcat website.

The photo “Compressed Car” was uploaded to Flickr by marcovdz and used under the terms of a Creative Commons license.

 

 

original:http://techxplorer.com/2010/09/17/enabling-gzip-compression-in-tomcat/