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

Source: Internet
Author: User

Home (PC browser or mobile browser) when the response to Tomcat is received, the header information is usually taken with content-length, and typically the client will not begin parsing until the content-length-length data has been accepted. On Tomcat, in the process of page processing, the data that needs to be out.print is placed in the cache and then returned to the client at once.

Another situation is that the header information does not exist in the content-length, instead of the tansfer-encoding:chunked, this header message means that response content will be sent by Tomcat into a piece, The client does not need to wait until the content has been transmitted before parsing the content. Because the length of the data being transmitted at this time is not predictable, there is no content-length meaning to exist tansfer-encoding:chunked.

So the question is, how do you control whether Tomcat responds to Content-length or tansfer-encoding in the application?

We all know that the built-in object is out, and when it calls print or write, it writes the data to the internal buffer instead of outputting it directly to the client. The content-length of the Response Header is actually calculating the data length of the buffer. So when does he output to the client? There are several situations:

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

2. The out attribute AutoFlush to false, which throws an exception IOException if the data exceeds the buffer's capacity.

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

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

Reprint: Http://blog.csdn.net/shootyou/archive/2011/01/13/6135669.aspx

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

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

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.