本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7594069
建立时间:2006年5月29日




[Apache(jakarta)]在Tomcat上直接配置GZIP压缩
软件技术

lhwork 发表于 2007/1/19 9:16:20

之前我写了两篇关于apache的mod_deflate模块的文章 1. 关于mod_deflate安装的问题处理 2. 使用mod_deflate提升网页浏览的速度 这两篇文章主要介绍在apache http server下如何使用mod_deflate模块来压缩http响应内容,最大限度的减小网络流量,然而当我们仅仅是使用tomcat服务器时候,更多的文章是通过自行编写一些代码来实现输出内容的压缩,其实tomcat本身在5.0版本以后是支持内容压缩的,它使用的是gzip的压缩格式,我们先来看Tomcat文档中对下面两个配置的注解(红色粗体字部分) compressableMimeType The value is a comma separated list of MIME types for which HTTP compression may be used. The default value is text/html,text/xml,text/plain. compression The Connector may use HTTP/1.1 GZIP compression in an attempt to save server bandwidth. The acceptable values for the parameter is "off" (disable compression), "on" (allow compression, which causes text data to be compressed), "force" (forces compression in all cases), or a numerical integer value (which is equivalent to "on", but specifies the minimum amount of data before the output is compressed). If the content-length is not known and compression is set to "on" or more aggressive, the output will also be compressed. If not specified, this attribute is set to "off". 这两个配置是在servere.xml中的Connector部分,第一个配置是指定Tomcat压缩哪些内容,第二个配置是指示Tomcat是否启用压缩,默认是关闭的。所以假设我们要让Tomcat在默认的8080端口上的输出内容进行压缩,我们的配置应该是:     <Connector port="8080" protocol="HTTP/1.1"                maxThreads="150" connectionTimeout="20000"                redirectPort="8443" compression="on"/> 一旦启用了这个压缩功能后,我们怎么来测试压缩是否有效呢?首先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也进行压缩:)


阅读全文(3466) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.320 second(s), page refreshed 144755427 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号