Tomcat启用Gzip压缩进行优化

对于Tomcat5.0以后的版本是支持对输出内容进行压缩的。使用的是gzip压缩格式

<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"    
    -->  

从上面的第8行内容可以看出,要使用gzip压缩功能,你可以在Connector实例中加上如下属性即可:

compression="on" 打开压缩功能
compressionMinSize="2048" 启用压缩的输出内容大小,这里面默认为2KB
noCompressionUserAgents="gozilla, traviata" 对于以下的浏览器,不启用压缩
compressableMimeType="text/html,text/xml" 压缩类型

我这里的配置内容为:

<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"    
    -->  

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

程序代码:

package com.yovisun.test;   
  
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://wordpresstest.yovisun.com/js/jquery.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();   
        }   
    }   
}  
/usr/themes/NexTSun/static/images/yovisun-weixin-share.jpg
赞 (0) 分享
声明:原创文章,欢迎转载,请以 超链接 的形式注明 作者标题原始出处查看许可协议
标题Tomcat启用Gzip压缩进行优化 | 作者:YoviSun
地址http://www.yovisun.com/archive/tomcat-gzip.html
相关文章:
添加新评论
选择表情
手机扫描二维码访问