HTTP request Headers Overview
HTTP client programs, such as browsers, must indicate the type of request (usually get or post) when sending a request to the server. If necessary, the client program can also choose to send additional request headers. Most request headers are not required,
Except Content-length. Content-length must appear for the POST request.
Here are some of the most common request headers
Accept: The MIME type acceptable to the browser.
Accept-charset: The acceptable character set of the browser.
Accept-encoding: The way the browser can decode data encoding, such as gzip. The servlet can return a GZIP-encoded HTML page to a browser that supports gzip. In many cases this can reduce download time by 5 to 10 times times.
Accept-language: The type of language the browser wishes to use when the server is able to provide more than one language version.
Authorization: Authorization information, which typically occurs in an answer to the Www-authenticate header sent to the server.
Connection: Indicates whether a persistent connection is required. If the servlet sees the value here; Keep-alive ", or see that the request is using an HTTP 1.1 (HTTP 1.1 is persistent by default), it can take advantage of the persistent connection
The advantages of the connection, when the page contains multiple elements (such as applets, pictures), significantly reduce the time required to download. To do this, the servlet needs to send a content-length header in the answer, and the simplest implementation is:
Write the content to Bytearrayoutputstream first, and then calculate its size before formally writing the content.
Content-length: Represents the length of the request message body.
Cookie: This is one of the most important request header information
From: The email address of the requesting sender, used by some special Web client, is not used by the browser.
Host: The hosts and ports in the initial URL.
If-modified-since: Returns 304 only if the requested content has been modified after the specified date; Not Modified "answer.
Pragma: Specify; No-cache "value indicates that the server must return a refreshed document, even if it is a proxy server and has a local copy of the page.
Referer: Contains a URL from which the user accesses the currently requested page from the page represented by the URL.
User-agent: Browser type, this value is useful if the content returned by the servlet is related to the browser type.
UA-PIXELS,UA-COLOR,UA-OS,UA-CPU: A nonstandard request header sent by some versions of Internet Explorer to indicate screen size, color depth, operating system, and CPU type.
For a complete, detailed description of the HTTP header, see the HTTP specification for http://www.w3.org/Protocols/.
HTTP Reply Header Overview (HttpServletResponse)
The HTTP response of a Web server is generally composed of a status line, one or more answer headers, a blank line, and a content document. Setting the HTTP answer header is often combined with the status code in the Set status line. For example, there are good
A few representations; the status code of the document position has changed "is 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 answer header even when there is no status code that has special meanings set. The answer header can be used to complete: Set a cookie, specify the date of modification, instruct the browser to refresh the page at a specified interval, declare the text
The length of the file to take advantage of persistent HTTP connections, ... And so many other tasks.
The most common method for setting the answer header is the setheader of the HttpServletResponse, which has two parameters that represent the name and value of the answer header, respectively. Similar to setting the status code, setting the answer header should send any document content
Prior to.
The Setdateheader method and the Setintheadr method are specifically designed to set up an answer header that contains a date and integer value, which avoids the hassle of converting Java time to a GMT time string, which avoids the hassle of converting integers to strings.
HttpServletResponse also offers a number of settings
setContentType: Sets the Content-type header. This method is used by most servlets.
Setcontentlength: Sets the content-length header. This function is useful for browsers that support persistent HTTP connections.
Addcookie: Set a cookie (there is no Setcookie method in the Servlet API, because the answer often contains multiple Set-cookie headers).
Also, as described in the previous section, the Sendredirect method sets the location header when the status Code 302 is set.
For a detailed and complete description of the HTTP header, see the http://www.w3.org/Protocols/specification.
Response Header Description
Which request methods (such as Get, post, and so on) 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. Using gzip to compress documents can significantly reduce the download time of HTML documents. Java's
Gzipoutputstream can be easily gzip compressed, but it is only supported on Unix Netscape and IE 4, ie 5 on Windows. Therefore, the servlet should look through the accept-encoding header (that is,
Request.getheader ("accept-encoding")) check if the browser supports GZIP, return the GZIP-compressed HTML page for the gzip-enabled browser, and return to the normal page for other browsers.
Content-length represents 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, and when you're done, look at its
Size, then put the value into the Content-length header and finally send the content through Bytearraystream.writeto (Response.getoutputstream ().
Content-type indicates what MIME type the subsequent document belongs to. The servlet defaults to Text/plain, but it usually needs to be explicitly specified as text/html. Since content-type are often set up, httpservletresponse
For a dedicated method of Setcontenttyep.
Date the current GMT time. You can use Setdateheader to set this header to avoid the hassle of converting the time format.
When should Expires think that the document has expired so that it does not cache it?
Last-modified The last modification time of the document. 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
Back to a 304 (not Modified) state. Last-modified can also be set using the Setdateheader method.
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 have the browser read the specified page by SetHeader ("Refresh", "5;; Url=http://host/path")
Surface. Note that this functionality is usually achieved by setting the 〈meta http-equiv= "Refresh" content= "5;;url=http://host/path" in the head area of the HTML page, because automatic refresh or redirection is not allowed for
It is important to use CGI or servlet HTML writers. For Servlets, however, it is more convenient to set the refresh header directly. Note that the meaning of refresh is; n seconds to refresh this page or to access the specified page "instead of; every n seconds brush
New page 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 using the refresh header or 〈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.
Server name. The servlet generally does not set this value, but is set by the Web server itself.
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
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).
Basic concepts of 1,HTTP requests
Tcp/upd/http
*2,http The meaning of the request header and the response header
Request Header:
accept:text/html,image/* (types that the browser can receive)
Accept-charset:iso-8859-1 (the type of encoding the browser can receive)
Accept-encoding:gzip,compress (The browser can receive the compression encoding type)
ACCEPT-LANGUAGE:EN-US,ZH-CN (language and country type that the browser can receive)
HOST:WWW.IT315.ORG:80 (browser-requested host and Port)
If-modified-since:tue, Jul 18:23:51 GMT (one page cache time)
referer:http://www.it315.org/index.jsp (which page the request came from)
user-agent:mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0) (browser-related information)
Cookies: (Information sent by the browser staging server)
Connection:close (1.0)/keep-alive (1.1) (features of the HTTP request version)
Date:tue, 18:23:51 GMT (time to request the site)
Response header:
location:http://www.it315.org/index.jsp (control which page the browser displays)
Server:apache Tomcat (type of server)
Content-encoding:gzip (compressed encoding method sent by the server)
CONTENT-LENGTH:80 (the server sends the displayed byte code length)
CONTENT-LANGUAGE:ZH-CN (language and country name of the server sending content)
Content-type:image/jpeg; Charset=utf-8 (Type and encoding type of content sent by the server)
Last-modified:tue, 18:23:51 GMT (the last time the server was modified)
refresh:1;url=http://www.it315.org (Control browser 1 seconds after the forwarding URL points to the page)
Content-disposition:attachment; Filename=aaa.jpg (server control browser send download open file)
transfer-encoding:chunked (server chunked pass data to client)
SET-COOKIE:SS=Q0=5LB_NQ; Path=/search (server sends cookie-related information)
Expires: 1 (server control browser do not cache Web pages, default is cache)
Cache-control:no-cache (server control browser do not cache Web pages)
Pragma:no-cache (server control browser do not cache Web pages)
Connection:close/keep-alive (features of the HTTP request version)
Date:tue, 18:23:51 GMT (response to site time)
*3,http application scenarios for request headers and response headers
Refresh, regardless of whether or not there is a cache, to fetch data from the server
HTTP Request Header & Response header