Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The principle and configuration method of tomcat setting gzip Compression

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Brief introduction of principle

HTTP compression can greatly improve the speed of browsing the website. its principle is that after the client requests the corresponding resources from the server, the resource files are compressed from the server and then output to the client, and the browser of the client is responsible for decompressing and browsing. Compared with the normal browsing process HTML, CSS,Javascript, Text, it can save about 40% of the traffic. More importantly, it can compress dynamically generated web pages, including CGI, PHP, JSP, ASP, Servlet,SHTML and so on, with high compression efficiency.

Configuration method

Future versions of Tomcat5.0 support compressing the output, using the gzip compression format.

Modify% TOMCAT_HOME%/conf/server.xml, and the revision node is as follows:

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

Compression= "on" enable compression function compressionMinSize= "50" enable compressed output content size, default is 2KB noCompressionUserAgents= "gozilla, traviata" for the following browsers, compression is not enabled for compressableMimeType= "text/html,text/xml,text/javascript,text/css,text/plain" which resource types require compression

Testing method

With the compression feature TOMCAT enabled, how can we test whether the compression is effective?

First of all, Tomcat determines whether the browser supports compression according to the accept-encoding in the browser request header. If this value contains gzip, it means that the browser supports browsing of gzip compressed content. We can use two methods to verify whether the compression is effective.

Request directly through the browser

You can directly access the server with compression configuration enabled through the browser, and then check the captured packets through the packet grab tool. If there are a lot of content that you do not understand, it means that compression has been enabled.

Simulate the request through the program

Let's write a simple test program in httpclient with the following code:

@ 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 this junit program to see what it outputs, if the output is some garbled, and the length of the printed content is far less than the actual length, it means that our configuration is effective, through some other verification tools, we will find that the browsing speed of the website will be significantly improved.

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

Summary

The above is the principle and configuration method of tomcat setting gzip compression introduced by the editor to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to the website!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report