The response of the HTTP protocol

Source: Internet
Author: User

I. Basic format of the response

The response basic format includes the status line , the response header , and the response entity Three parts, for example:

http/1.1 OK
server:jsp3/2.0.6
date:wed, Geneva 08:19:53 GMT
Content-type:image/gif
content-length:43
Connection:keep-alive
Last-modified:mon, 03:15:11 GMT
expires:wed, Geneva 08:22:23 GMT
age:447
cache-control:max-age=600
Accept-ranges:bytes
Timing-allow-origin:http://www.baidu.com

(The response entity is not affixed, a bunch of garbled)

1. Status line:

On the first line, including the protocol and its version number, status code, status description, for example:
http/1.1 OK

2. Response header:

The content that precedes a separate empty line after the request line.
The response header is detailed:
Server: Servers name. The servlet generally does not set this value, but is set by the Web server itself.


Date: The current GMT time. You can use Setdateheader to set this header to avoid the hassle of converting the time format.


Content-type: Indicates what MIME type the following document belongs to. The servlet defaults to Text/plain, but it usually needs to be explicitly specified as text/html. Because Content-type is often set up, HttpServletResponse provides a dedicated method setContentType.


Content-length: Indicates the content length. This data is only required if the browser is using a persistent HTTP connection. If you want to take advantage of the persistent connection, you can write the output document to Bytearrayoutputstram, look at its size when done, then put that value into the Content-length header and finally pass the Bytearraystream.writeto ( Response.getoutputstream () Send content.

Connection: Indicates whether a persistent connection is required. If the servlet sees the value here as "keep-alive", or sees the request using an HTTP 1.1 (HTTP 1.1 is persistent by default), it can take advantage of the persistent connection, when the page contains multiple elements (such as applets, pictures), Significantly reduce the time it takes to download. To do this, the servlet needs to send a content-length header in the answer, and the simplest implementation is to write the content to Bytearrayoutputstream first and then calculate its size before formally writing the content.

Last-modified: The last time the document was changed. The customer can provide a date through the If-modified-since request header, which is treated as a conditional get, and only documents that have been modified later than the specified time are returned, otherwise a 304 (not Modified) state is returned. Last-modified can also be set using the Setdateheader method.

Expires: When should I think that the document has expired so that it is not cached?

More common than the above, some of the following are uncommon:
Allow: Which request methods are supported by the server (such as GET, post, etc.).

Content-encoding: The Encoding (Encode) method of the document. The content type specified by the Content-type header can be obtained only after decoding. Using gzip to compress documents can significantly reduce the download time of HTML documents. Java's gzipoutputstream can be easily gzip compressed, but only on Unix Netscape and IE 4, ie 5 on Windows. Therefore, the servlet should check whether the browser supports gzip by looking at the accept-encoding header (that is, Request.getheader ("accept-encoding")). Returns the gzip-compressed HTML page for a browser that supports gzip, returning a normal page for another browser.

Location: Indicates where the customer should go to extract the document. Location is usually not set directly, but by HttpServletResponse's Sendredirect method, which sets the status code to 302.

Refresh: Indicates how much time the browser should refresh the document, in seconds. In addition to refreshing the current document, you can also pass SetHeader ("Refresh", "5; Url=http://host/path ") lets the browser read the specified page. Note This functionality is usually done by setting the HTML page in the head area of the <meta http-equiv= "Refresh" content= "5; Url=http://host/path "> is implemented because automatic refresh or redirection is important for HTML writers who cannot use CGI or servlets. For Servlets, however, it is more convenient to set the refresh header directly. Note that the meaning of refresh is "refresh this page after n seconds or visit the specified page" instead of "refresh this page every n seconds or visit the specified page". Therefore, continuous refresh requires a refresh header to be sent each time, and sending a 204 status code prevents the browser from continuing to refresh, whether it is using the refresh header or the <meta http-equiv= "Refresh" ...>. Note that the refresh header is not part of the HTTP 1.1 formal specification, but rather an extension, but both Netscape and IE support it.

Set-cookie: Sets the Cookie associated with the page. The servlet should not use Response.setheader ("Set-cookie", ...), but should use the dedicated method Addcookie provided by HttpServletResponse. See below for a discussion of cookie settings.
What type of authorization information should www-authenticate customers provide in the authorization header? This header is required in an answer that contains a 401 (unauthorized) status line. For example, Response.setheader ("Www-authenticate", "BASIC realm=\" Executives\ ""). Note that the servlet generally does not handle this, but instead gives the Web server a special mechanism to control access to password-protected pages (for example,. htaccess).

3. Response Entity:

After the empty line, usually a bunch of garbled, do not understand. Directly copy garbled and then save to the suffix of the response, can directly get the file.

The response of the HTTP protocol

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.