8. set HTTP response header _ MySQL

Source: Internet
Author: User
8.1HTTP response header overview the Web server's HTTP response consists of the following items: One status line, one or more response headers, one blank line, and content documentation. Setting the HTTP response header is often combined with the status code in the set status line. For example, several status codes that indicate "the document Location has changed" are accompanied by a Location header, while 401 (Unauthoriz 8.1 HTTP response header overview)

The HTTP response of a Web server generally consists of one status line, one or more response headers, one blank line, and content document. Setting the HTTP response header is often combined with the status code in the set status line. For example, several status codes indicating "the document Location has changed" are accompanied by a Location header, while the 401 (Unauthorized) status code must be accompanied by a WWW-Authenticate header.

However, it is useful to specify the response header even if the status code with special meanings is not set. The response header can be used to complete: Set the Cookie, specify the modification date, instruct the browser to refresh the page at the specified interval, and declare the document length so that persistent HTTP connections can be used ,...... And many other tasks.

The most common method to set the response header is the setHeader of HttpServletResponse. this method has two parameters, indicating the name and value of the response header, respectively. Similar to the set status code, the set response header should be performed before any document content is sent.

The setDateHeader and setIntHeadr methods are used to set the response headers containing dates and integers. The former avoids the trouble of converting Java time into GMT time strings, the latter avoids the trouble of converting integers into strings.


HttpServletResponse also provides many easy methods to set common response headers, as shown below:

SetContentType: set the Content-Type header. Most servlets use this method.
SetContentLength: Set the Content-Length header. This function is useful for browsers that support persistent HTTP connections.
AddCookie: Set a Cookie (the Servlet API does not have the setCookie method, because the response usually contains multiple Set-Cookie headers ).
In addition, the Location header is also set when the sendRedirect method sets status code 302.
8.2 Common response headers and their meanings

For more information about HTTP headers, see http://www.w3.org/protocols.

Response header description
Which request methods (such as GET and POST) are supported by the Allow server ).
The Encoding (Encode) method of the Content-Encoding document. The Content Type specified by the Content-Type header can be obtained only after decoding. Gzip compression can significantly reduce the download time of HTML documents. Java GZIPOutputStream can be easily compressed by gzip, but it is supported only by Netscape on Unix and IE 4 and IE 5 on Windows. Therefore, Servlet should view the Accept-Encoding header (request. getHeader ("Accept-Encoding") checks whether the browser supports gzip, returns the gzip-Compressed HTML page for a browser that supports gzip, and returns a common page for other browsers.
Content-Length indicates the Content Length. This data is required only when the browser uses a persistent HTTP connection. If you want to take advantage of persistent connections, you can write the output document to ByteArrayOutputStram, view its size, put the value in the Content-Length header, and finally use byteArrayStream. writeTo (response. content sent by getOutputStream.
Content-Type indicates the MIME Type of the subsequent document. Servlet is text/plain by default, but it must be explicitly specified as text/html. Because Content-Type is often set, HttpServletResponse provides a dedicated method setContentTyep.
The current GMT time of Date. You can use setDateHeader to set this header to avoid the trouble of converting the time format.
When should Expires consider that the document has expired and thus it is no longer cached?
The Last modification time of the Last-Modified document. You can use the If-Modified-Since request header to provide a date. this request is considered as a condition GET. only documents whose modification time is later than the specified time will be returned, otherwise, a 304 (Not Modified) status is returned. Last-Modified can also be set using the setDateHeader method.

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.