TOMCAT configuration gzip compression improves the speed of browsing Web sites _tomcat

Source: Internet
Author: User
Tags tomcat

HTTP compression can greatly improve the speed of browsing the Web site, the principle is that the client requests the Web page, from the server side of the Web page file compression, and then download to the client, by the client's browser is responsible for decompression and browsing. Compared to the normal browsing process HTML, css,javascript, Text, it can save about 40% of the traffic. More importantly, it can compress dynamically generated Web pages, including CGI, PHP, JSP, ASP, servlet,shtml, and so on, and the compression efficiency is amazing.

A later version of Tomcat5.0 is to support the compression of the output using the GZIP compression format

Here is the original content of the $tomcat_home$/conf/server.xml in tomcat5.5.20
< Connector port = "maxhttpheadersize" = "8192"
MaxThreads = "Minsparethreads" = "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 8th line above you can see that to use the GZIP compression feature, you can add the following properties to the connector instance

1) compression= "on" to turn on the compression function
2 compressionminsize= "2048" to enable 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 here is:

<connector port= "maxhttpheadersize=" 8192 "maxthreads=", "minsparethreads=" maxsparethreads= "
75"
enablelookups= "false" redirectport= "8443" acceptcount= "20000"
connectiontimeout= " 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 your could set the following properties:

compression= "on" 
compressionmins Ize= "2048" 
nocompressionuseragents= "Gozilla, Traviata" compressablemimetype= "Text/html,text/xml"
-->

Once this compression function is enabled, how do we test that the compression is valid? First Tomcat is based on the accept-encoding in the browser request header to determine whether the browser supports compression, if this value contains gzip, indicating that the browser supports gzip compressed content browsing, So we can use HttpClient to write a simple test program like this.

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 Httpclien T ();
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.executemethod (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 ();}}

Execute this test program, see what it output is what content, if the output is some garbled, and the length of the printed content is far less than the actual length, then congratulations, your configuration is effective, you will find your site browsing faster than before.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.