How Tomcat enables gzip for static file compression

Source: Internet
Author: User
Tags configuration settings tomcat server
Web page Compression

is a protocol that is shared between Web servers and browsers, which means that Web servers and browsers must support the technology, and now popular browsers are supported, including IE, FireFox, Opera, and so on; servers are tomcat, Apache, and IIS. The consultation process is as follows: 1. First, the browser requests a URL address and sets the property accept-encoding value to gzip in the requested header (head), indicating that the browser supports both gzip and deflate compression methods (in fact Deflat E is also using the GZIP compression protocol). 2, the WEB server received the request to determine whether the browser support compression, if supported on the transmission of compressed response content, otherwise the transfer is not compressed content. 3, the browser to obtain the response content, determine whether the content is compressed, if it is uncompressed, and then display the response page content.

So if you enable Tomcat's Gzip compressed Web page data, just modify the configuration in Tomcat, the client and the server do not have to make other changes. Principle Introduction

HTTP compression can greatly improve the speed of browsing the Web site, it is the principle, in the client request the server corresponding resources, from the server side of the resource file compression, and then output to the client, by the client's browser is responsible for decompression and browsing. Compared to the normal browsing process HTML, css,javascript, Text, it can save about 40% of the traffic. More importantly, it can be dynamically generated, including CGI, PHP, JSP, ASP, servlet,shtml and other output pages can also be compressed, compression efficiency is also high. 1, Compression:off/on/force (do not enable/enable compressed text content/Force compression all requests include pictures). 2, Compressablemimetype: "Text/html,text/xml,text/plain" (this is the default value, specify the type of request document that needs to be compressed). 3, nocompressionuseragents: Specify the browser agent does not need compression, using a comma interval. 4, compressionminsize:2048 to less than the size of the file is not Compressed (unit: B, is the default is 2K). Tomcat Configuration

Find in the Tomcat Server.xml file, for example:

    <connector port= "8080" protocol= "http/1.1" connectiontimeout= "20000" 
               redirectport= 
               "8443" uriencoding= "Utf-8" "
               compression="       
               on "compressionminsize=" 8190 "nocompressionuseragents=   
               " Gozilla, Traviata "  compressablemimetype=" text/html,text/xml,application/javascript,text/javascript,text/css,text/ Plain,application/json "usesendfile= false"  
               />
Verify that it is in effect

After enabling the Tomcat compression feature, how do we test if the compression is valid? method One: Use browser test

Press F12 to open the browser's console (here is the WebKit kernel browser), click on the network bar, and then refresh the page, click on the start of the configuration settings need to gzip any of the file types, appear in the following figure two o'clock is a success:

method Two: Use HttpClient to detect whether gzip compression is effective

The jar package to introduce: Httpcore-4.2.4.jar,httpclient-4.2.5.jar
The download address for a httpclient jar package (Httpclient-4.2.5.jar) is provided here: Httpclient-4.2.5.jar

Package Com.test.app;
Import Org.apache.http.HttpResponse;
Import org.apache.http.client.HttpClient;
Import Org.apache.http.client.ResponseHandler;
Import Org.apache.http.client.methods.HttpGet;
Import Org.apache.http.impl.client.BasicResponseHandler;

Import org.apache.http.impl.client.DefaultHttpClient; /** * Class Description: Test gzip configured successfully * @author:p GF * @date: 2016-8-29 Morning 11:05:34/public class Test {public static void main (
      String[] args {String uri = "Http://localhost:8080//test//defaults//js//jquery-1.9.1.min.js";
      HttpClient httpclient = new Defaulthttpclient ();
      HttpGet get = new HttpGet (URI);

      responsehandler<string> ResponseHandler = new Basicresponsehandler ();
        try {//Set analog head get.setheader ("accept-encoding", "gzip,deflate"); Get.setheader ("User-agent", "mozilla/5.0 (Windows NT 6.1) applewebkit/537.36 (khtml, like Gecko) chrome/29.0.1547.57

        safari/537.36 "); String content = Httpclient.execute (GET, responseHandler); SYSTEM.OUT.PRINTLN (content);
        If gzip is in effect, this will print out garbled, otherwise it will print out jquery.js content/**/HttpResponse response = Httpclient.execute (get);
        Long CLen = Response.getentity (). Getcontentlength (); System.out.println (CLen);
        If gzip takes effect, the length value printed here will be-1 or a value that is much smaller than the original file size} catch (Exception e) {//Ignore ...} finally {   
      Httpclient.getconnectionmanager (). Shutdown (); }

    }
}

Execute this test program, if gzip effective, here will print out garbled, otherwise will print out the content of Jquery.js, then the configuration is effective.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.