Latest web development tutorials

HTTP response headers

HTTP request header provides information about the request, response, or other transmission entities.

In this section we will introduce specific HTTP response headers.

Response header Explanation
Allow

Which servers support the request method (such as GET, POST, etc.).

Content-Encoding

Document encoding (Encode) method. Only after decoding can get Content-Type header specifies the type of content. Use gzip compressed files can dramatically reduce the download time of HTML documents. Java's GZIPOutputStream can be easily gzip compression, but only Netscape and IE on Windows Unix on 4, IE 5 only support it. Thus, Servlet should be by looking at the Accept-Encoding header (ie request.getHeader ( "Accept-Encoding")) to check whether the browser supports gzip, gzip browser support gzip compression by the return of the HTML page, to return to normal as other browsers page.

Content-Length

Representing the content length. If the browser to use persistent HTTP connections need this data only when. If you want to take advantage of persistent connections, can be written to the output document ByteArrayOutputStream, completed view the size, and then put that value into the Content-Length header, and finally sending content via byteArrayStream.writeTo (response.getOutputStream ().

Content-Type

It indicates that the following documentation is what MIME type. Servlet default is text / plain, but usually need to explicitly specified as text / html. Because often set Content-Type, so the HttpServletResponse provides a dedicated approach setContentType.

Date

The current GMT time. You can set this setDateHeader head to avoid the trouble of switching time format.

Expires

What should be considered when the document has expired, so that it is no longer the cache?

Last-Modified

Last modified time of the document. Customers can provide through the If-Modified-Since request header a date, the request will be considered as a condition GET, only changes later than the specified time, the document will be returned, otherwise returns a 304 (Not Modified) status. Last-Modified setDateHeader method can also be used to set up.

Location

It indicates that the client should go to fetch documents. Location is usually not set directly, but through the sendRedirect HttpServletResponse method, which also set a status code of 302.

Refresh

It indicates the browser should refresh the document after how much time, in seconds. In addition to refresh the current document outside, you can also setHeader ( "Refresh", "5; URL = http: // host / path") let the browser reads the specified page.
Note that this feature is usually by setting the HTML page HEAD area <META HTTP-EQUIV = "Refresh" CONTENT = "5; URL = http: // host / path"> achieved, it is because, for the automatic refresh or redirect those who can not use CGI or Servlet writers of HTML is very important. However, Servlet is set directly Refresh head more convenient.

Note Refresh the meaning of "N seconds after refresh the page or go to the specified page" instead of "refresh the page or access the specified page every N seconds." Thus, the continuous refresh requires each to send a Refresh header, and send 204 status code, it can prevent the browser continue to refresh, use the Refresh either the head or the <META HTTP-EQUIV = "Refresh" ...>.

Note Refresh HTTP 1.1 header is not part of the formal specification, but rather an extension, but Netscape and IE support it.

Server

Server name. Servlet generally do not set this value, but is set by the Web server itself.

Set-Cookie

Settings and Cookie page associated. Servlet should not be used response.setHeader ( "Set-Cookie", ...), but you should use special methods addCookie HttpServletResponse provides. See the discussion about Cookie settings below.

WWW-Authenticate

Customers should provide information on what types of authorization Authorization header? In reply contains 401 (Unauthorized) status line in the header is required. For example, response.setHeader ( "WWW-Authenticate", "BASIC realm = \" executives\ "").
Note Servlet generally not be treated in this regard, but to give special mechanisms of the Web server to control access to password-protected pages (such as .htaccess).