SoFunction
Updated on 2025-03-08

Tomcat configures gzip compression to improve website browsing speed

HTTP compression can greatly improve the speed of browsing websites. Its principle is that after the client requests the web page, the web page file is compressed from the server and then downloaded to the client. The client's browser is responsible for decompressing and browsing. Compared with the normal browsing process HTML, CSS, Javascript, and Text, it can save about 40% of traffic. More importantly, it can also compress dynamically generated web pages, including CGI, PHP, JSP, ASP, Servlet, SHTML, etc., with amazing compression efficiency.

1. For Tomcat 5.0 and later versions, it supports compression of output content. The gzip compression format is used.

Below is the original content of $tomcat_home$/conf/ in tomcat5.5.20
< Connector port ="80" maxHttpHeaderSize ="8192"
maxThreads ="150" minSpareThreads ="25" maxSpareThreads ="75"
enableLookups ="false" redirectPort ="8443" acceptCount ="100"
connectionTimeout ="20000" disableUploadTimeout ="true" URIEncoding ="utf-8" />
<!-- Note : To disable connection timeouts, set connectionTimeout value
to 0 -->

<!-- Note : To use gzip compression you could set the following properties :

compression="on"
compressionMinSize="2048"
noCompressionUserAgents="gozilla, traviata"
compressableMimeType="text/html,text/xml"

--> From the above line 8, you can see that to use gzip compression function, you can add the following attributes to the Connector instance

1) compression="on" Turn on the compression function
2) compressionMinSize="2048" Enables the compressed output content size, which defaults to 2KB
3) noCompressionUserAgents="gozilla, traviata" For the following browsers, compression is not enabled
4) compressableMimeType="text/html,text/xml" Compression type (default is text/html, text/xml, text/plain)

My configuration content here is:

<Connector port="80" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="utf-8"
compression="on" 
compressionMinSize="2048" 
noCompressionUserAgents="gozilla, traviata" 
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain" />
<!-- Note : To disable connection timeouts, set connectionTimeout value
to 0 -->

<!-- Note : To use gzip compression you could set the following properties :

compression="on" 
compressionMinSize="2048" 
noCompressionUserAgents="gozilla, traviata" 
compressableMimeType="text/html,text/xml"
-->

Once this compression function is enabled, how can we test whether the compression is effective? First of all, Tomcat determines whether the browser supports compression function based on the accept-encoding in the browser request header. If this value contains gzip, it means that the browser supports browsing gzip compressed content, so we can use httpclient to write such a simple test program.

import ;
import ;

public class HttpTester {

public static void main(String[] args) throws Exception{
HttpClient http = new HttpClient();
GetMethod get = new GetMethod("/js/");
try{
("accept-encoding", "gzip,deflate");
("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Alexa Toolbar; Maxthon 2.0)");
int er = (get);
if(er==200){
(());
String html = ();
(html);
(().length);
}
}finally{
();
}
}

}

Execute this test program to see what it outputs. If the output is some garbled code and the length of the printed content is much smaller than the actual length, then congratulations, your configuration has taken effect and you will find that your website browsing speed is much faster than before.