Objective:
GZIP encoding on the HTTP protocol is a technique used to improve the performance of Web applications. High-traffic Web sites often use gzip compression technology to make users feel faster. This generally refers to the WWW server installed in a feature, when someone to access the server's website, the server of this feature will compress the content of the Web page to be displayed in the browser of the visiting computer. Generally, the plain text content can be compressed to 40% of the original size. So the transmission is fast, The effect is that when you click on the URL, it will show up quickly. This, of course, also increases the load on the server. This function module is installed in the general server.
First, how to use gzip
The principle of gzip work is simple, the HTTP request header adds a "accept-encoding:gzip" command to inform the server that gzip compression is required, and the server receives the request through the "Content-encoding:gzip" To inform the client of Gzip decompression.
For Tomcat this is:
<connector executor="Tomcatthreadpool"Port=" the"Protocol="http/1.1"ConnectionTimeout="20000"Redirectport="8443"uriencoding="UTF-8"Compression=" on"compressionminsize="2048"nocompressionuseragents="Gozilla, Traviata"Compressablemimetype="Text/html,text/xml,application/javascript,text/css,text/plain,text/json"/>
The connector is configured as above in Server.xml in Tomcat:
compression="on"
Turn on compression
compressionMinSize="2048"
Files larger than 2KB are compressed
noCompressionUserAgents="gozilla, traviata"
, for both browsers, do not compress
compressableMimeType="text/html,text/xml,application/javascript,text/css,text/plain,text/json"
Indicates support for compression of HTML, XML, JS, CSS, JSON and other file formats
Second, disable proxy caching using Cache-control
Import Java.io.ioexception;import Javax.servlet.filter;import javax.servlet.filterchain;import Javax.servlet.filterconfig;import Javax.servlet.servletexception;import Javax.servlet.servletrequest;import Javax.servlet.servletresponse;import Javax.servlet.http.HttpServletResponse; Public classCachecontrolfilter implements Filter { Public voidDoFilter (servletrequest request, servletresponse response, Filterchain chain) throws IOException, Servlete xception {HttpServletResponse resp=(httpservletresponse) response;//Resp.setheader ("Expires", "Tue, Jul 2001 06:00:00 GMT");//Resp.setdateheader ("last-modified", New Date (). GetTime ());//Resp.setheader ("Cache-control", "No-store, No-cache, Must-revalidate, max-age=0, post-check=0, pre-check=0"); Resp.setheader ("Cache-control","max-age=0, Private");//Resp.setheader ("Pragma", "No-cache");Chain.dofilter (request, response); } @Override Public voiddestroy () {} @Override Public voidInit (Filterconfig arg0) throws servletexception {}}
Added in Web. xml:
<filter> <filter-name>cacheControlFilter</filter-name> <filter-class> com.honzh.common.filter.cachecontrolfilter</filter-class> </filter> < filter-mapping> <filter-name>cacheControlFilter</filter-name> <url-pattern> /* </url-pattern> </filter-mapping>
Web server configuration gzip compression improves site performance