HTTP压缩可以大大提高浏览网站的速度,它的原理是,在客户端请求网页后,从服务器端将网页文件压缩,再下载到客户端,由客户端的浏览器负责解压缩并浏览。相对于普通的浏览过程HTML、css、Javascript、Text,它可以节省40%左右的流量。更为重要的是,它可以对动态生成的,包括CGI、PHP、JSP、ASP、Servlet、SHTML等输出的网页也能进行压缩,压缩效率惊人。
目前实现gzip压缩有2种方法:
方法一、是有的容器(服务器)提供的功能,但这个局限于特定容器。比如apache+tomcat或者resin-pro版;
方法二、自定义Filter压缩GZIP;
本项目中采用的是方法二,该方法有个缺陷就是并不是所有的浏览器都支持GZIP解压缩,如果浏览器支持GZIP解压缩,会在请求报头的Accept-Encoding里包含gzip。这是告诉服务器浏览器支持GZIP解压缩,因此如果用程序控制压缩,为了保险起见,还需要判断浏览器是否发送accept-encoding:gzip报头,如果包含了该报头,才执行压缩。
本项目采用的是tk-filters来压缩文件的,去http://sourceforge.net/projects/filterlib/
网站下载tk-filters文件,然后解压开来,将里边的tk-filters.jar文件复制到Web工程的WebRoot/WEB-INF/lib目录下,在web.xml文件中加上几个filter,代码如下所示:
<!-- JS文件压缩过滤器配置 begin-->
<!-- 压缩过滤器 GZIPFilter Definition -->
<filter>
<filter-name>GZipFilter</filter-name>
<filter-class>com.tacitknowledge.filters.gzipfilter.GZIPFilter</filter-class>
</filter>
<!-- 压缩过滤器 CacheFilter Definition -->
<filter>
<filter-name>CacheFilter</filter-name>
<filter-class>com.tacitknowledge.filters.cache.CacheHeaderFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>GZipFilter</filter-name>
<url-pattern>*.js</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>GZipFilter</filter-name>
<url-pattern>*.css</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CacheFilter</filter-name>
<url-pattern>*.js</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CacheFilter</filter-name>
<url-pattern>*.css</url-pattern>
</filter-mapping>
<!-- JS文件压缩过滤器配置 end-->
其中GZipFilter定义的是压缩过滤器,用于压缩JS及CSS文件,CacheFilter定义的是缓存过滤器,在第一次请求后会将mapping的js和css文件缓存在本地,这样第二次请求的时候就不用再下载这些文件,以减少传输的数据量,提高访问的速度。
接下来创建一个属性文件,文件名为:tk-filters.properties,将这个文件放在classes目录下,如果用IDE的话,就直接在src目录中创建就行了,IDE会自动将src中的文件映射到classes目录中的。
tk-filters.properties文件的内容如下:
#
# This properties file controls the behavior of the various
# filters available in the Tacit Knowledge filters package.
#
# Each filter has its own set of configuration directives,
# prefixed with the filter name, that controls that specific
# filter's behavior
#
#########################################################################
# ClusterCheck
#########################################################################
# A frequent problem when clustering is that applications use the session
# in a non-clusterable way, so The "ClusterCheckFilter" instruments the
# application server's sessions with checks to see if this is a problem.
ClusterCheckFilter.Enabled=false
# Its possible to check for modifications to session objects after
# they have been set in to the session. This is a problem for sessions
# that are replicated in a copy-on-write fashion
ClusterCheckFilter.UnsetModificationsCheck=false
# Its possible to check serialized size to ensure high performance clustering
ClusterCheckFilter.ByteSizeCheck=false
# Aggregate size is important for containers that serialize the whole session
# every time. An example would be a database-backed session store.
# An aggregate size limit will also cap the maximum RAM used by sessions,
# allowing you to quantify the RAM necessary for peak loads.
ClusterCheckFilter.AggregateByteSizeLimit=30720
# Attribute size is important because each time an attribute is put in a
# session, it has to be serialized and persisted to a cluster peer, or
# to a database (depending on clustering implementations). Thus, very large
# session attributes will be a performance problem.
ClusterCheckFilter.AttributeByteSizeLimit=20480
# ClusterCheck errors can return a 500 error to the client
# in order to have "fail-fast" behavior, if this is turned on.
# This is good for test machines, but is usually too aggressive for production.
ClusterCheckFilter.ClientError=true
#########################################################################
#########################################################################
# GZIPFilter
#########################################################################
# A performance boost can be achieved by sending data from the application
# server to the client using the Gzip encoding. This incurs a small CPU
# cost to gain a large network benefit. The GZIPFilter, when enabled,
# transparently Gzip encodes all data after it leaves the application,
# but before its transmitted to the client.
# WARNING: GZIPFilter is currently not recommended for production use.
# It does not send all data under certain conditions.
GZIPFilter.Enabled=true
# Its possible for the GZIPFilter to log statistics about the compression
# ratios and byte savings it is achieving. This turns that on or off.
GZIPFilter.LogStats=false
#########################################################################
#########################################################################
# CacheFilter
#########################################################################
# A server can send expiration headers to the client, enabling the client
# to confidently cache certain pieces of static content. This eliminates
# unnecessary conditional GETs from the client to validate the freshness of
# content. If the application is on a server that doesn't do that, this
# filter can be enabled and mapped to static content (images, javascript,
# css files, etc), potentially reducing network traffic a great deal.
CacheFilter.Enabled=true
# This is the number of minutes the client will wait before verifying the
# freshness of a piece of content.
CacheFilter.ExpirationMinutes=15
#########################################################################
这个文件的主要功能是开启压缩和缓存功能。
下面是用firefox跑的结果: