Tomcat Response header information (Http Response header) Content-length and transfer-encoding

Source: Internet
Author: User
Tags flush

Welcome reprint, but please indicate the source thank you: http://blog.csdn.net/shootyou/archive/2011/01/13/6135669.aspx

Let's talk about the principle:

When a client (PC browser or mobile browser) receives a response from Tomcat, the header is usually content-length, and typically the client will begin parsing after receiving the content-length length of data. On Tomcat, the data that needs to be out.print is placed in the cache during page processing and then returned to the client at a one-time.

Another situation is that there is no content-length in the header information, instead of the tansfer-encoding:chunked, which means that the contents of the response are sent by Tomcat into a piece, The client also does not need to wait until the content is transferred before parsing its contents. Because the length of the data being transmitted at this time is unpredictable, there is no content-length meaning in the presence of tansfer-encoding:chunked.

So the question is, specifically in the application of how to control the Tomcat response is content-length or tansfer-encoding it.

We all know that built-in objects out, in the call to print or write to the internal buffer in the first write data, rather than directly output to the client. The content-length of the Response Header actually calculates the data length of the buffer. So when does he output to the client? There are several situations:

1. Out of the property AutoFlush is true, then when the buffer (the default size is 8 * 1024) of the data is full, Tomcat automatically to the client flush data once, then the buffer is reset. The inevitable content-length will not be taken. So this time the repsonse header becomes the tansfer-encoding:chunked.

2. Out of the property autoflush to False, if the data exceeds the capacity of the buffer, this time will throw an exception IOException.

3. If the data is within the capacity range of the buffer, then the content-length can be computed and the header information will be content-length.

4. If manually called Out.flush (), then the data in the buffer will be immediately output to the client, this time the response data has not yet been transmitted, so this transmission can also be considered as a block transmission. Repsonse header nature is tansfer-encoding:chunked.

From the above several situations can be seen, once called the Out.flush,buffer was reset, content-length can not calculate, repsonse header must be tansfer-encoding:chunked.

In addition, if there is a need to write their own clients, you can refer to the following: http://hfutxf.javaeye.com/blog/807647, which will speak of the tansfer-encoding:chunked when a need to consider the situation, I did not verify.

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.