Tomcat configuration gzip compression, tomcatgzip Compression

Source: Internet
Author: User

Tomcat configuration gzip compression, tomcatgzip Compression
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.


How to configure and start Tomcat in zip version?

Install JDK first. You do not need to set environment variables in the current version as long as the version is installed, for a non-installed version, right-click my computer and choose Properties> advanced> environment variables> system variables to add the following environment variables (assuming that your j2sdk is installed in c: \ j2sdk1.5.0):

JAVA_HOME = c: \ j2sdk1.5.0
Classpath =.; % JAVA_HOME % \ lib \ dt. jar; % JAVA_HOME % \ lib \ tools. jar;
(.; Must not be less, because it represents the current path)
Path = % JAVA_HOME % \ bin

Decompress TOMCAT and add the following environment variables to the system variables (assuming that your tomcat is installed in c: \ tomcat ):

CATALINA_HOME = c: \ tomcat
CATALINA_BASE = c: \ tomcat

Modify the classpath in the environment variable, and append servlet. jar under the common \ lib directory under the tomat installation directory (you can append it according to the actual situation) to the classpath. The modified classpath is as follows:

Classpath =.; % JAVA_HOME % \ lib \ dt. jar; % JAVA_HOME % \ lib \ tools. jar; % CATALINA_HOME % \ common \ lib \ servlet. jar;

Start tomcat and access localhost: 8080 in IE. If you see the welcome page of tomcat, the installation is successful.

How to install the tomcat6034 compressed version and configure environment variables? The details are a bit messy on the Internet ,,

It is best to download the decompressed version and install it directly in Myeclipse without configuring ports and environment variables.

Related Article

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.