- 论坛徽章:
- 0
|
Tomcat配置文件:
connectionTimeout="20000"
port="8080" protocol="HTTP/1.1"
redirectPort="8443"
compression="on"
compressionMinSize="1024"
noCompressionUserAgents="gozilla, traviata"
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain" />
compression是否启用压缩,on为启用,off为关闭.
compressionMinSize启用压缩的输出内容大小,默认为2KB,修改设置为了1024KB.
noCompressionUserAgents不启用压缩的浏览器,主要是对于一些老浏览器.不清楚这两个是什么浏览器,做为示例.
compressableMimeType要压缩文件的MIME类型.
一旦启用了这个压缩功能后,怎么来测试压缩是否有效呢?首先Tomcat是根据浏览器请求头中的accept-encoding来判断浏览器是否支持压缩功能,如果这个值包含有gzip,就表明浏览器支持gzip压缩内容的浏览,所以可以用httpclient来写一个这样的简单测试程序
程序代码:
package com.liusoft.dlog4j.test;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
/**
* HTTP客户端测试类
* @author liudong
*/
public class HttpTester {
/**
* @param args
*/
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.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();
}
}
} 执行这个测试程序,看看它所输出的是什么内容,如果输出的是一些乱码,以及打印内容的长度远小于实际的长度,那么恭喜你,配置生效了,会发现网站的浏览速度比以前快多了。
另外最好对网站所用的javascript和css也进行压缩:)
相关:
Apache mod_deflate 配置
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/1676/showart_1911873.html |
|