原文地址:http://benx.iteye.com/blog/561998
在tomcat中配置gzip的方法是在conf/server.xml文件中把以前的配置修改成
- <Connector
- port= "8080" maxHttpHeaderSize= "8192"
- maxThreads= "150" minSpareThreads= "25" maxSpareThreads= "75"
- enableLookups= "false" redirectPort= "8443" acceptCount= "100"
- connectionTimeout= "20000" disableUploadTimeout= "true"
- compression= "on"
- compressionMinSize= "2048"
- noCompressionUserAgents= "gozilla, traviata"
- compressableMimeType= "text/html,text/xml,text/javascript,text/css,text/plain" />
- />
经测试在没有压缩之前的大小为1.5M 在压过之后只有10K,可能是和有很多重复数据有关,响应速度也有以前的2.5秒到275ms
配置后的头文件你会发现成这样了
- 响应头信息
- Server Apache-Coyote/1.1
- Set-Cookie JSESSIONID= 072D9B189C4AA9DA4D4AD0B6C2C89E50; Path=/test
- Content-Type text/html; charset= ISO-8859-1
- Transfer-Encoding chunked
- Content-Encoding gzip
- Vary Accept-Encoding
- Date Mon, 04 Jan 2010 04:49:38 GMT
- 请求头信息
- Host localhost:8080
- User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6
- Accept text/html,application/xhtml+xml,application/xml; q= 0.9,*/*; q= 0.8
- Accept-Language zh-cn,zh; q= 0.5
- Accept-Encoding gzip,deflate
- Accept-Charset GB2312,utf-8; q= 0.7,*; q= 0.7
- Keep-Alive 300
- Connection keep-alive
- Cookie JSESSIONID= 1C8DB59AC74495495ED374B13C3D930A
- Cache-Control max-age= 0
没有配之前为
- 响应头信息
- Server Apache-Coyote/1.1
- Content-Type text/html; charset= ISO-8859-1
- Transfer-Encoding chunked
- Date Mon, 04 Jan 2010 04:54:00 GMT
- 请求头信息
- Host localhost:8080
- User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6
- Accept text/html,application/xhtml+xml,application/xml; q= 0.9,*/*; q= 0.8
- Accept-Language zh-cn,zh; q= 0.5
- Accept-Encoding gzip,deflate
- Accept-Charset GB2312,utf-8; q= 0.7,*; q= 0.7
- Keep-Alive 300
- Connection keep-alive
- Cookie JSESSIONID= 072D9B189C4AA9DA4D4AD0B6C2C89E50
- Cache-Control max-age= 0
在jsp中默认都是使用缓存的,如果不想使用可以在jsp页面中添加
- <%
- response.setHeader( "Pragma", "No-cache");
- response.setHeader( "Cache-Control", "no-cache");
- response.setDateHeader( "Expires", 0);
- %>
注意,这里只是对页面不使用缓存,图片、css、js还是可以使用缓存的
如果想控制某个类型不使用缓存可以使用过滤器