背景:HTTP 压缩可以大大提高浏览网站的速度,它的原理是,在客户端请求网页后,从服务器端将网页文件压缩,再下载到客户端,由客户端的浏览器负责解压缩并浏览。相对于普通的浏览过程HTML ,CSS,Javascript , Text ,它可以节省40%左右的流量。更为重要的是,它可以对动态生成的,包括CGI、PHP , JSP , ASP , Servlet,SHTML等输出的网页也能进行压缩,压缩效率惊人。
第一步:修改$tomcat_home$/conf/server.xml
将
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
修改为
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"
compression="on"
compressionMinSize="2048"
noCompressionUserAgents="gozilla, traviata"
compressableMimeType="text/html,text/xml,text/plain,text/css,
text/javascript,text/json,application/x-javascript,
application/javascript,application/json"/>
注意要添加application/javascript,tomcat5用的可能是text/javascript,但tomcat7用的是application/javascript
第二步:用浏览器访问你重新部署的程序,打开开发者工具,进入Network面板,刷新页面,然后选择一个js文件点击,查看 Headers下面的Response Headers,里面会标明Encoding为gzip,那么恭喜你成功了。
以上是服务器自动帮你压缩文件,但如果文件比较大,你希望提前压缩后,到时候服务器自动读取你压缩的gzip文件而不是由服务器来压缩,怎么做呢?
第一步:实现filter类,让filter类来读取你压缩的gzip文件
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Logger;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GzipFilter implements Filter {
private ServletContext ctx;
private Logger logger = Logger.getLogger(GzipFilter.class.getName());
private String contextPath;
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
String uri = req.getRequestURI();
String accept = req.getHeader("Accept-Encoding");
InputStream in = null;
contextPath = ctx.getContextPath();
uri = uri.substring(contextPath.length());
if (accept != null && accept.contains("gzip") && (in = ctx.getResourceAsStream(uri + ".gz")) != null) {
logger.info("start getting gzip file "+uri);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] b = new byte[1024 * 8];
int read = 0;
while ((read = in.read(b)) >= 0) {
bout.write(b, 0, read);
}
in.close();
res.setHeader("Content-Encoding", "gzip");
res.setContentType("application/javascript;charset=UTF-8");
res.setContentLength(bout.size());
ServletOutputStream out = res.getOutputStream();
out.write(bout.toByteArray());
out.flush();
logger.info("finish getting gzip file "+uri);
return;
} else {
chain.doFilter(request, response);
}
}
@Override
public void init(FilterConfig config) throws ServletException {
ctx = config.getServletContext();
}
}
第二步:配置web.xml
添加如下片段即可
<!-- read the precompressed gzip js file-->
<filter>
<filter-name>gzip</filter-name>
<filter-class>GzipFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>gzip</filter-name>
<url-pattern>*.js</url-pattern>
</filter-mapping>
第三步:压缩你的js文件,比如a.js为a.js.gzip文件
第四步:用chrome验证是否成功
分享到:
相关推荐
开启TOMCAT6的GZIP压缩功能,在客户端请求网页后,从服务器端将网页文件压缩,再下载到客户端,由客户端的浏览器负责解压缩并浏览。相对于普通的浏览过程HTML ,CSS,Javascript , Text ,它可以节省40%左右的流量。...
使用gzip压缩,能够减少数据传输大小,加快网页或APP加载速度。很多大站都开启了gzip压缩,不过也有很多网站并没有开启gzip压缩,本文档使用截图和文字的方式详细讲解了压缩数据的配置和过程,希望对您有益!
tomcat开启gzip,如何开启tomcat 中gzip文件1
tomcat设置gzip压缩共2页.pdf.zip
很多大站都开启了gzip压缩,不过也有很多网站并没有开启gzip压缩,上次看了一篇文章说开启gzip压缩后对搜索引擎不友好,但从带宽和流量的角度来看,还是有必要开启gzip压缩的。 对于tomcat7服务器,打开conf文件夹下...
tomcat7_win64解压版tomcat7_win64解压版tomcat7_win64解压版
tomcat7.0.73压缩版
如果发现内容没有被压缩,可以考虑调整compressionMinSize大小,如果请求资源小于这个数值,则不会启用压缩
包含tomcat7,tomcat8,tomcat9,解压、在eclipse配置好即可。
主要介绍了应用Tomcat进行多端口域名访问,并配置开启gzip压缩方法,需要的朋友可以参考下
tomcat7源码下载tomcat7源码下载tomcat7源码下载tomcat7源码下载tomcat7源码下载tomcat7源码下载tomcat7源码下载tomcat7源码下载
主要介绍了tomcat设置gzip压缩的原理及配置方法,需要的朋友可以参考下
tomcat7免安装版点击上传资源即表示您确认该资源不违反资源分享的使用条款,并且您拥有该资源的所有版权或者上传资源的授权
绿色版tomcat 7 无需安装,将war包扔进webapp中直接启动就可以。
linux服务器64位的tomcat7下载,本人正在使用这个tomcat服务器,亲测有效
针对tomcat7存在漏洞升级补丁,漏洞影响的tomcat7版本为Apache Tomcat 7.0.0 to 7.0.93
tomcat7 tomcat7 tomcat7 tomcat7 tomcat7 tomcat7 tomcat7
apache-tomcat-7.0.25 tomcat7