HTTP compression can greatly improve the speed of website browsing. The principle is that after the client requests a web page, the server compresses the web page file and downloads it to the client, the client browser is responsible for Uncompressing and browsing. Compared with ordinary browsing processes such as HTML, CSS, JavaScript, and text, it can save about 40% of the traffic. More importantly, it can compress the pages dynamically generated, including CGI, PHP, JSP, ASP, Servlet, shtml, and other output pages. The compression efficiency is amazing.
1. For Versions later than tomcat, the output content can be compressed using the gzip compression format.
The following is the original content of $ tomcat_home $/CONF/server. XML in tomcat5.5.20. 1 <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 cocould set the following properties:
Compression = "on"
Compressionminsize = "2048"
Nocompressionuseragents = "gozilla, Traviata"
Compressablemimetype = "text/html, text/XML"
--> Above8th rowsYou can add the following attributes to the connector instance to use the gzip compression function.
1) compression = "on" enable the compression function
2) compressionminsize = "2048" enables the size of compressed output content. The default value is 2 kb.
3) nocompressionuseragents = "gozilla, Traviata" for the following browsers, compression is not enabled
4) compressablemimetype = "text/html, text/XML" compression type (default: text/html, text/XML, text/plain)
The 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 cocould 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, Tomcat determines whether the browser supports Compression Based on the accept-encoding in the browser request header. If this value contains gzip, it indicates that the browser supports browsing of gzip compressed content, so we can use httpclient to write such a simple test program.
Import org. Apache. commons. httpclient. httpclient;
Import org. Apache. commons. httpclient. Methods. getmethod;
Public class httptester {
Public static void main (string [] ARGs) throws exception {
Httpclient HTTP = new httpclient ();
Getmethod get = new getmethod ("http://www.dlog.cn/js/prototype.js ");
Try {
Get. addrequestheader ("Accept-encoding", "gzip, deflate ");
Get. addrequestheader ("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Alexa toolbar; Maxthon 2.0 )");
Int ER = http.exe cutemethod (get );
If (ER = 200 ){
System. Out. println (get. getresponsecontentlength ());
String html = Get. getresponsebodyasstring ();
System. Out. println (HTML );
System. Out. println (html. getbytes (). Length );
}
} Finally {
Get. releaseconnection ();
}
}
}
Run this test program to see what it outputs. If the output is garbled and the length of the printed content is much smaller than the actual length, congratulations, if your configuration takes effect, you will find that your website browsing speed is much faster than before.
Tomcat configuration gzip Compression