站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题在Tomcat上直接配置GZIP压缩

在Tomcat上直接配置GZIP压缩

摘要: 在Tomcat上直接配置GZIP压缩

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="513" height="86" align="left" valign="top">

之前我写了两篇关于apache的mod_deflate模块的文章

</td> <td width="171" valign="top"> </td> </tr> </table>

一旦启用了这个压缩功能后,我们怎么来测试压缩是否有效呢?首先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也进行压缩:)

</td> </tr> <tr>


↑返回目录
前一篇: java3D魔方源代码
后一篇: 编写可以响应运行时变化的代码