5.1 HTTP Request First Overview (HTTPSERVLETREQUEST)
The HTTP client (such as a browser) must specify the request type when sending a request to the server (typically GET or POST). If necessary, the client can also choose to send other request heads. Most requested heads are not required, except for Content-Length. Content-length must appear for POST requests.
Here are some of the most common request heads.
Accept: The browser acceptable MIME type.
Accept-charset: The browser acceptable character set.
Accept-encoding: Browser can decode data encoding, such as Gzip. Servlets are able to return to Gzip's browsers to returns the Gzip encoded HTML page. This can reduce download time by 5 to 10 times in many cases.
Accept-language: The language type of the browser wants to use when the server provides more than one language version.
Authorization: Authorized information, usually appears in a response to the WWW-Authenticate header sent to the server.
Connection: Indicates if a persistent connection is required. If the servlet sees the value here "Keep-alive" or see the request is HTTP 1.1 (HTTP 1.1 default persistent connection), it can take advantage of the persistent connection, when the page contains multiple elements (for example, Applet, image), significantly reduce the time you need. To achieve this, servlet needs to send a Content-Length header in your response. The simplest implementation method is: first writing the content into ByteArrayoutputStream, and then calculate its size before formal writing.
Content-length: Indicates the length of the text of the request message.
Cookie: This is one of the most important request headers
From: Request the sender's Email address, use some special web clients, the browser will not use it.
Host: The host and port in the initial URL.
If-modified-since: Only returns it only when the requested content is modified after the specified date, otherwise returns the 304 "Not Modified" response.
Pragma: Specifies the "no-cache" value indicates that the server must return a refresh document, even if it is a proxy server and already has a local copy of the page.
Referr: Contains a URL that uses the user from the page represented by the URL to access the currently requested page.
User-agent: Browser type, if the content returned by servlet is very useful to the browser type.
UA-PIXELS, UA-Color, UA-OS, UA-CPU: Non-standard request headers sent by some versions of IE, represent screen size, color depth, operating system, and CPU type.
For complete, detailed descriptions of HTTP headers, see http specifications http://www.w3.org/protocols/.
8.1 Overview of HTTP Respondents (HTTPSERVLETRESPONSE)
The WEB server's HTTP response is generally composed of the following: a status line, one or more responses, one blank, content document. Setting the HTTP response head tends to combine the status code in the setting status line. For example, several status code indicating "document location has changed" accompanied by a location head, and 401 (Unauthorized) status code must be accompanied by a WWW-Authenticate header. However, even when there is no special sense of state code, the specified response head is also useful. The response head can be used to complete: set cookies, specify the change date, indicating that the browser refreshes the new page according to the specified intervals, declares the length of the document to take advantage of lasting HTTP connection, ... and more many other tasks.
Setting the most commonly used method of respondent is HTTPSERVLETRESPONSE's setHead, which has two parameters, indicating the name and value of the response head, respectively. Similar to setting status code, setting the response heads should be performed before sending any document content.
The setDateHeader method and the setInTheadr method are specifically used to set the response to the date and integer value. The former avoids the trouble of converting Java time into a GMT time string, and the latter avoids the inclusion of integers into a string.
HTTPSERVLETRESPONSE also provides a number of settings ?? 鹜 鹜 虮惴 虮惴?? 缦 filter?
SetContentType: Set the Content-Type header. Most servlets use this method. SetContentLength: Sets Content-Length Head. 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 response often contains multiple set-cookie headers). In addition, as described in the previous section, the SendRedirect method sets the local code 302 will also set the location header.
For detailed and complete descriptions of HTTP headers, see http://www.w3.org/protocols/ specification.
Respondent Description ALOW server supports which request methods (such as get, post, etc.). The encoding method of the Content-Encoding document. Only the content type specified by the Content-Type header can only be obtained after decoding. The use of GZIP compressed documents can significantly reduce the download time of the HTML document. Java's GzipOutPutStream can easily compress Gzip, but only IE 4 on the Netscape and Windows on UNIX, IE 5 supports it. Therefore, servlet should check if the browser supports Gzip by viewing an accept-encoding header ("Accept-encoding")), returning to the Gzip compressed HTML page for other browsers to return to the Gzip-compressed HTML page to support Gzip's browser. page. Content-length represents the length of the content. This data is only required when the browser uses a persistent HTTP connection. If you want to use the advantage of a lasting connection, you can write the output document to ByteArrayoutputstram. After the completion is complete, then put this value into the Content-Length header, and finally send content by ByteaRrayStream.Writto (response.getputstream (). Content. Content. Content. Content. Content -Type indicates what MIME type belongs later .Servlet defaults to text / plain, but it usually needs to be explicitly designated as Text / HTML. Since the Content-Type is often set, HttpServletResponse provides a dedicated method setContentTyep. Date current GMT time. You can use SetDateHeader to set this head to avoid the trouble of conversion time format. When you think that the document has expired, no longer cache the last change time of the last-modified document. Customer can pass if- The Modified-Since request header provides a date that will be considered a condition GET, which only returns a document that is later than the specified time, otherwise returns a 304 (not modified) state. Last-Modified is also available in the SetDateHeader method. Setting. Location means that where is the client to extract the document. Location is usually not directly set, but through the HTTPSERVLETRESPONSE's sendRedirect method, the method simultaneously sets the status code to 302. Refresh indicates how much time it should be refreshed after the browser, in seconds In addition to refreshing the current document, you can also let the browser read the specified page via SetHeader ("Refresh", "5; URL = http: // host / path"). Note that this feature is usually passed Automatically refresh or redirect HTML that cannot use CGI or Servlet The writer is very important. However, for the servlet, it is more convenient to set up the refresh header. Note that refresh is "N second, refresh this page or accesses the specified page" instead of "Refreshing this page every N seconds or accesses the specified Page ". Therefore, continuous refresh requirements send a refresh header each time, and send 204 status code, you can prevent the browser from continuing to refresh, whether it is using a refresh header Also .