启用Tomcat6的隐藏Gzip压缩功能

其實是 tomcat 6 把注释整個拿掉,让大家以为TomcatGzip不再支持,其实不然,大家可以看一下http://tomcat.apache.org/tomcat-6.0-doc/config/http.html就知道,Tomcat依然支持这个功能。<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

背景:
HTTP压缩可以大大提高浏览网站的速度,它的原理是,在客户端请求网页后,从服务器端将网页文件压缩,再下载到客户端,由客户端的浏览器负责解压缩并浏览。相对于普通的浏览过程HTML ,CSS,Javascript , Text,它可以节省40%左右的流量。更为重要的是,它可以对动态生成的,包括CGIPHP , JSP , ASP , Servlet,SHTML等输出的网页也能进行压缩,压缩效率惊人
一,对于Tomcat5.0以后的版本是支持对输出内容进行压缩的. 使用的是gzip压缩格式
下面是tomcat<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><chsdate w:st="on" isrocdate="False" islunardate="False" day="30" month="12" year="1899">5.5.20</chsdate>中的$tomcat_home$/conf/server.xml的原内容

1 <?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape> < Connector port ="80" maxHttpHeaderSize ="8192"
2 <shape id="_x0000_i1026" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>maxThreads ="150" minSpareThreads ="25" maxSpareThreads ="75"
3 <shape id="_x0000_i1027" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>enableLookups ="false" redirectPort ="8443" acceptCount ="100"
4 <shape id="_x0000_i1028" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>connectionTimeout ="20000" disableUploadTimeout ="true" URIEncoding ="utf-8" />
5 <shape id="_x0000_i1029" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape> <!-- Note:Todisableconnectiontimeouts,setconnectionTimeoutvalue
6 <shape id="_x0000_i1030" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>to0 -->
7 <shape id="_x0000_i1031" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>
8 <shape id="_x0000_i1032" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape> <!-- Note:Tousegzipcompressionyoucouldsetthefollowingproperties:
9 <shape id="_x0000_i1033" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>
10 <shape id="_x0000_i1034" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>compression="on"
11 <shape id="_x0000_i1035" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>compressionMinSize="2048"
12 <shape id="_x0000_i1036" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>noCompressionUserAgents="gozilla,traviata"
13 <shape id="_x0000_i1037" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>compressableMimeType="text/html,text/xml"
14 <shape id="_x0000_i1038" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape> -->

从上面的8内容可以看出,要使用gzip压缩功能,你可以在Connector实例中加上如下属性即可
1) compression="on"
打开压缩功能
2) compressionMinSize="2048"
启用压缩的输出内容大小,这里面默认为2KB
3) noCompressionUserAgents="gozilla, traviata"
对于以下的浏览器,不启用压缩
4) compressableMimeType="text/html,text/xml"
 压缩类型

我这里的配置内容为:

1<shape id="_x0000_i1039" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape><Connectorport="80"maxHttpHeaderSize="8192"
2<shape id="_x0000_i1040" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>maxThreads="150"minSpareThreads="25"maxSpareThreads="75"
3<shape id="_x0000_i1041" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>enableLookups="false"redirectPort="8443"acceptCount="100"
4<shape id="_x0000_i1042" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>connectionTimeout="20000"disableUploadTimeout="true"URIEncoding="utf-8"
5<shape id="_x0000_i1043" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>compression="on"
6<shape id="_x0000_i1044" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>compressionMinSize="2048"
7<shape id="_x0000_i1045" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>noCompressionUserAgents="gozilla,traviata"
8<shape id="_x0000_i1046" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"/>
9<shape id="_x0000_i1047" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape><!--Note:Todisableconnectiontimeouts,setconnectionTimeoutvalue
10<shape id="_x0000_i1048" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>to0-->
11<shape id="_x0000_i1049" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>
12<shape id="_x0000_i1050" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape><!--Note:Tousegzipcompressionyoucouldsetthefollowingproperties:
13<shape id="_x0000_i1051" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>
14<shape id="_x0000_i1052" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>compression="on"
15<shape id="_x0000_i1053" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>compressionMinSize="2048"
16<shape id="_x0000_i1054" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>noCompressionUserAgents="gozilla,traviata"
17<shape id="_x0000_i1055" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>compressableMimeType="text/html,text/xml"
18<shape id="_x0000_i1056" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>-->
19<shape id="_x0000_i1057" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://www.blogjava.net/Images/OutliningIndicators/None.gif" src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.gif"></imagedata></shape>

一旦启用了这个压缩功能后,我们怎么来测试压缩是否有效呢?首先Tomcat是根据浏览器请求头中的accept-encoding来判断浏览器是否支持压缩功能,如果这个值包含有gzip,就表明浏览器支持gzip压缩内容的浏览,所以我们可以用httpclient来写一个这样的简单测试程序

窗体顶端

程序代码:

packagecom.liusoft.dlog4j.test;

importorg.apache.commons.httpclient.HttpClient;
importorg.apache.commons.httpclient.methods.GetMethod;

/**
*HTTP
客户端测试类
*@authorliudong
*/

publicclassHttpTester{

/**
*@paramargs
*/

publicstaticvoidmain(String[]args)throwsException{
HttpClienthttp=newHttpClient();
GetMethodget=newGetMethod("http://www.dlog.cn/js/prototype.js");
try{
get.addRequestHeader("accept-encoding","gzip,deflate");
get.addRequestHeader("user-agent","Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.0;AlexaToolbar;Maxthon2.0)");
inter=http.executeMethod(get);
if(er==200){
System.out.println(get.getResponseContentLength());
Stringhtml=get.getResponseBodyAsString();
System.out.println(html);
System.out.println(html.getBytes().length);
}
}finally{
get.releaseConnection();
}
}

}

窗体底端

执行这个测试程序,看看它所输出的是什么内容,如果输出的是一些乱码,以及打印内容的长度远小于实际的长度,那么恭喜你,你的配置生效了,你会发现你网站的浏览速度比以前快多了。

另外你最好对网站所用的javascriptcss也进行压缩:)

如果你懒得用上面这个程序测试,好吧,用这个也行:http://gzip.zzbaike.com/

你可能感兴趣的:(tomcat,C++,.net,C#,asp.net)