Servlet customer request processing: HTTP Request Header httpservletrequest Interface Application

Source: Internet
Author: User

The access to the Request Header allows the servlet to perform many optimizations and create an efficient servlet.

1. Read the HTTP request header in the servlet-httpservletrequest Interface 

Read the HTTP header in the servlet and call the getheader method of httpservletrequest.

Getheader (string header name) method: return the specified header information provided in the customer request. The result is a character and the parameter is case insensitive. Otherwise, null is returned. Make sure that the host is not empty before you call this method to obtain the result.

View request header information:

Getcookie method: return the content of the cookie header, Which is parsed and stored in the array of cookie objects.

Getauthtype and getremoteuser Methods: read part of the content in the Authorization header;

Getdateheader and getintheader Methods: Read the specified header and return the date value or integer value.

Enumerate the header names in a specific request:

Getheadernames () method: returns an enumeration object.

Obtain the request's main command line information:

Getmethod: return the request method. The request method is usually get or post, but may also be head, put, or delete.

Getrequesturi method: return the URI (URI is the part of the URL following the host and port to the form data ).

Getrequestprotocol method: returns the third part of the Request command, which is generally "HTTP/1.0" or "HTTP/1.1 ".

Retrieve all request header information

First, use getheadernames () to obtain the header name enumeration object in the request. Traverse the enumeration object and use the getheader method to obtain the request header information.

Ii. Overview of HTTP request headers

1. The HTTP client program (such as a browser) must specify the request type (usually get or post) when sending a request to the server ). If necessary, the customer program can also choose to send other request headers. Most request headers are not required, except Content-Length. Content-Length must appear for post requests.

2. The following are some of the most common request headers:

Accept: the MIME type acceptable to the browser. Servlet checks accept to determine which format is returned to client resources. IE6 and 7 bugs: re-loading the accept header sent by the page is incorrect, but it is correct in the initial request.

Accept-charset: the acceptable character set of the browser.

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

Accept-language: the type of language that the browser wants to use when the server can provide more than one language version.

Authorization: authorization information, usually displayed in the response to the www-Authenticate header sent by the server.

Connection: Indicates whether a persistent connection is required. If the servlet sees that the value here is "keep-alive", or the request uses HTTP 1.1 (HTTP 1.1 performs a persistent connection by default), it can take advantage of the advantages of persistent connections, when a page contains multiple elements (such as an applet or image), the download time is significantly reduced. To achieve this, the servlet needs to send a Content-Length header in the response. The simplest method is to write the content into bytearrayoutputstream first, then, calculate the size of the content before writing it.

Content-Length: the length of the Request Message Body.

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

From: the e-mail address of the Request sender, which is used by some special Web client programs and not used by the browser.

HOST: host and port in the initial URL.

If-modified-since: it is returned only when the requested content is modified after the specified date. Otherwise, the 304 "not modified" response is returned.

Pragma: specifying the "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 you can access the current requested page.

User-Agent: browser type. This value is useful if the content returned by the servlet is related to the browser type. Use the User-Agent only when not required. Check whether it is null when using it. To distinguish between Netscape and IE, check MSIE instead of "Mozilla ". This header can be added and Servlet cannot distinguish this situation.

UA-pixels, UA-color, UA-OS, UA-CPU: non-standard request headers sent by some versions of IE, indicating screen size, color depth, operating system, and cputype. For more information about HTTP headers, see http://www.w3.org/protocols.

3. compress the page to reduce the download time for the accept-encoding Application 

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

Servlet can return gzip-encoded HTML pages to a browser that supports gzip. In many cases, this can reduce the download time by 5 to 10 times. Before using any type of content encoding, check the accept-dncoding header. For long text pages, Gzip compression can effectively reduce the download time. Because not all browsers support gzip, the accept-encoding request header is set.

Use the servlet to check the accept-encoding request header to send a compressed web page to a browser that supports gzip compression (gzipoutputsteam output is encapsulated using printwrite), and send a standard web page (output using printwrite) to an unsupported web page ).

4. differentiate User-Agent applications of different browser types

User-Agent: browser type. This value is useful if the content returned by the servlet is related to the browser type.

Use the User-Agent only when not required. Check whether it is null when using it.

To distinguish between Netscape and IE, check MSIE instead of "Mozilla ". This header can be added and Servlet cannot distinguish this situation.

5. Customize the page Referer application based on the customer's arrival method

Referer: contains a URL from which you can access the current requested page.

Tasks that can be completed using this header:

Create a work or professional website and inherit the appearance of the website associated with it.

Use the name of the reference page to differentiate. Modify the page content based on the link from inside or outside the firewall. Note: like all other headers, it is easy to forge.

Provide a link for the user to return to the previous webpage.

Track the effectiveness of the title advertisement, or record the CTR of different websites for your advertisement.

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.