Servlet client Request Processing: HTTP request Header HttpServletRequest interface application

Source: Internet
Author: User

access to the request header enables the servlet to perform many optimizations and create an efficient servlet.

First, read the HTTP request header--httpservletrequest interface in the servlet

Reads the HTTP header in the servlet and invokes the HttpServletRequest GetHeader method.

GetHeader (String Header Name) method: Returns the specified header information provided in the customer request.   The result is a character, the argument is case-insensitive, or null is returned. Make sure the host is not empty before calling this method to obtain the result.

To view the request header information:

GetCookies method: Returns the contents of the cookie header, which is parsed and stored in an array of cookie objects.

Getauthtype and Getremoteuser methods: Read a part of the authorization head separately;

Getdateheader and Getintheader methods: reads the specified header, and then returns a date or integer value.

enumerates the header names in the current specific request:

Getheadernames () Method: Returns a enumeration object.

GET request main command line information:

GetMethod method: Returns the Request method, which is usually a get or post, but may also be head, put, or delete.

Getrequesturi method: Returns the URI (the part of the URL that precedes the host and port to the form data).

Getrequestprotocol method: Returns the third part of the request command, typically "http/1.0" or "http/1.1".

get all request header information

First, use Getheadernames () to get the header name enumeration object in the request. Traverses the enumeration object and uses the GetHeader method to get the request header information.

two, HTTP request header Overview

1,http client programs, such as browsers, must indicate the type of request (typically get or post) when sending a request to the server.   If necessary, the client can also choose to send another request header. Most request headers are not required, except for content-length. Content-length must appear for post requests.

2, here are some of the most common request headers:

Accept: The MIME type acceptable to the browser.     The servlet checks accept to determine which format is used to return to the client resource. IE6 and 7 Bugs: Reload the page to send the Accept header incorrectly, but it was correct in the original request.

Accept-charset: Browser-acceptable character set.

Accept-encoding: The data encoding method that the browser can decode, such as gzip. The servlet can return a GZIP-encoded HTML page to a browser that supports gzip.      In many cases this can reduce the download time by 5 to 10 times times. Be sure to check the accept-dncoding header before using any type of content encoding.

Accept-language: The kind of language that browsers want to use when the server can provide more than one language version.

Authorization: Authorization information that usually appears in the answer to the Www-authenticate headers sent to the server.

Connection: Indicates whether a persistent connection is required. If the servlet sees the value here as "keep-alive" or if it sees that the request uses HTTP 1.1 (HTTP 1.1 By default for persistent connections), it can take advantage of the persistent connection, when the page contains multiple elements (such as applets, pictures),    Significantly reduce the time required for downloading. To do this, the servlet needs to send a content-length header in the answer, the simplest way to do this is to write the content to Bytearrayoutputstream, and then calculate its size before the content is formally written out.

Content-length: Indicates the length of the request message body.

Cookie: This is one of the most important request header information.

From: Requests the sender's email address, which is used by some special Web client programs and is not used by browsers.

Host: Hosts and ports in the initial URL.

If-modified-since: Returns a 304 "not Modified" answer only if the requested content is returned after the specified date and after it has been modified.

Pragma: Specifies that the "No-cache" value indicates that the server must return a refreshed document, even if it is a proxy server and already has a local copy of the page.

Referer: Contains a URL in which the user accesses the currently requested page from the page represented by the URL.

User-agent: Browser type, which is useful if the content returned by the servlet is related to the browser type.      Use user-agent when you don't want to.      Check to see if it is null when used.      Distinguish Netscape and IE to check msie rather than "Mozilla". This header can be added and the servlet does not distinguish between the cases.

UA-PIXELS,UA-COLOR,UA-OS,UA-CPU: A nonstandard request header sent by some versions of IE to indicate screen size, color depth, operating system, and CPU type. For a complete and detailed description of HTTP headers, see the HTTP specification for http://www.w3.org/Protocols/.

third, compressed page to reduce download time accept-encoding application

Accept-encoding: The data encoding method that the browser can decode, such as gzip.

The servlet can return a GZIP-encoded HTML page to a browser that supports gzip.      In many cases this can reduce the download time by 5 to 10 times times.  Be sure to check the accept-dncoding header before using any type of content encoding. For longer text pages, using gzip compression can reduce download time effectively. Because not all browsers support gzip, the accept-encoding request headers are set up for support.

Check the accept-encoding request header through the servlet to send a compressed web page to a browser that supports gzip compression (using the Printwrite encapsulation Gzipoutputsteam output), Send a regular web page to an unsupported (using Printwrite output).

four, differentiate different browser types user-agent application

User-agent: Browser type, which is useful if the content returned by the servlet is related to the browser type.

Use user-agent when you don't want to. Check to see if it is null when used.

Distinguish Netscape and IE to check msie rather than "Mozilla". This header can be added and the servlet does not distinguish between the cases.

Five, according to the customer Arrival way Custom page Referer application

Referer: Contains a URL in which the user accesses the currently requested page from the page represented by the URL.

Use this header to accomplish the task:

Create a job or career site that follows the look and feel of links to its related sites.

Use the name of the reference page to differentiate. Change the content of the page depending on whether the link is from inside or outside the firewall. Note: As with all other headers, it is easy to forge.

Provide the appropriate link for the user to return to the previous page.

Track the validity of the title ad, or record the click rate of the different websites that display your ads.

Six, access to standard CGI variables

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.