Set the specific operation method for tomcat to enable gzip compression

  • 2020-04-01 02:12:20
  • OfStack

Introduction of the principle
HTTP compression can greatly improve the speed of browsing the website, its principle is, after the client requests the server corresponding resources, from the server to compress the resource file, and then output to the client, the client browser is responsible for decompression and browsing. It can save about 40% of the traffic compared to the normal browsing process of HTML,CSS,Javascript, Text. More importantly, it can dynamically generated, including CGI, PHP, JSP, ASP, Servlet,SHTML and other output pages can also be compressed, compression efficiency is also very high.

Configuration method
Later versions of Tomcat5.0 support compression of the output, using the gzip compression format.
 
Modify %TOMCAT_HOME%/conf/server.xml, and modify the node as follows:


 <Connector port="80" protocol="HTTP/1.1" 
    connectionTimeout="20000" 
    redirectPort="8443" executor="tomcatThreadPool" URIEncoding="utf-8" 
       compression="on" 
       compressionMinSize="50" noCompressionUserAgents="gozilla, traviata" 
       compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain" />

As you can see from the properties of the above node, to use the gzip compression feature, you need to add the following properties to the Connector node

The & # 8226; Compression ="on" to enable compression
The & # 8226; CompressionMinSize ="50" enables the compressed output content size, which defaults to 2KB
The & # 8226; NoCompressionUserAgents ="gozilla, traviata" compression is not enabled for the following browsers
The & # 8226; CompressableMimeType = "text/HTML, text/XML, text/javascript, text/CSS, text/plain" which the resource type need compression

The test method

With TOMCAT compression enabled, how can we test whether the compression works?
First, Tomcat determines whether the browser supports compression based on accept-encoding in the browser request header. If this value contains gzip, it indicates that the browser supports the browsing of gzip compressed content. We can use two methods to verify whether the compression is effective.
Request directly through the browser

You directly access the server through the browser enabled compression configuration, and then through the packet grab tool to see the captured packets, if there is a lot of content you do not understand, it means that the compression has been enabled.
Programmatically simulate the request
We use httpclient to write a simple test program, the code is as follows:


 @Test
  public void testGzip() {
    HttpClient httpClient = new HttpClient();
    GetMethod getMethod = new GetMethod("http://localhost/admin.jsp");
    try {
      getMethod.addRequestHeader("accept-encoding", "gzip,deflate");
      getMethod.addRequestHeader("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Alexa Toolbar; Maxthon 2.0)");
      int result = httpClient.executeMethod(getMethod);
      if (result == 200) {
        System.out.println(getMethod.getResponseContentLength());
        String html = getMethod.getResponseBodyAsString();
        System.out.println(html);
        System.out.println(html.getBytes().length);
      }
    } catch (HttpException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      getMethod.releaseConnection();
    }
  }

Execute the junit program and see what it outputs. If the output is some garbled code and the length of the printed content is much less than the actual length, then our configuration is working.

Note: if you find that the content is not compressed, consider adjusting the size of the compressionMinSize. If the requested resource is less than this, compression will not be enabled.


Related articles: