5. Read the HTTP Request Header

Source: Internet
Author: User

5.1 HTTP Request Header Overview

HTTP client programs (such as browsers) must specify the request type (usually GET or POST) when sending requests 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.

The following 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 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.
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. For more information, see the following section in Cookie processing.
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.
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.

5.2 read request headers in Servlet

It is very convenient to read the HTTP header in the Servlet. You only need to call the getHeader method of HttpServletRequest. If the specified header information is provided in the customer request, getHeader returns the corresponding string; otherwise, null is returned. Some header information is often used. They have a special access method: The getCookies method returns the content of the Cookie header, Which is parsed and stored in the array of Cookie objects, for more information, see the Cookie section. The getAuthType and getRemoteUser Methods read part of the Authorization header. The getDateHeader and getIntHeader Methods read the specified header and return the date value or integer value.

In addition to reading the specified header, you can use getHeaderNames to obtain an Enumeration object of all header names in the request.

Finally, in addition to viewing the request header information, we can also obtain some information from the request's main command line. The getMethod method returns the request method. The request method is usually GET or POST, but may also be HEAD, PUT, or DELETE. The getRequestURI method returns the URI (URI is the part of the URL from the host and port to the form data ). GetRequestProtocol returns the third part of the Request command, which is generally "HTTP/1.0" or "HTTP/1.1 ".

5.3 instance: Output all request headers

The following Servlet instance outputs all received request headers and their values in a table. In addition, the Servlet outputs three parts of the main request command: Request Method, URI, protocol/version.

ShowRequestHeaders. java
Package hall;

Import java. io .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Import java. util .*;

Public class ShowRequestHeaders extends HttpServlet {
Public void doGet (HttpServletRequest request,
HttpServletResponse response)
Throws ServletException, IOException {
Response. setContentType ("text/html ");
PrintWriter out = response. getWriter ();
String title = "show all request headers ";
Out. println (ServletUtilities. headWithTitle (title) +
"<Body bgcolor = \" # FDF5E6 \ "> \ n" +
"<H1 ALIGN = CENTER>" + title + "</H1> \ n" +
"<B> Request Method: </B>" +
Request. getMethod () + "<BR> \ n" +
"<B> Request URI: </B>" +
Request. getRequestURI () + "<BR> \ n" +
"<B> Request Protocol: </B>" +
Request. getProtocol () + "<BR> \ n" +
"<Table border = 1 ALIGN = CENTER> \ n" +
"<Tr bgcolor = \" # FFAD00 \ "> \ n" +
"<TH> Header Name <TH> Header Value ");
Enumeration headerNames = request. getHeaderNames ();
While (headerNames. hasMoreElements ()){
String headerName = (String) headerNames. nextElement ();
Out. println ("<TR> <TD>" + headerName );
Out. println ("<TD>" + request. getHeader (headerName ));
}
Out. println ("</TABLE> \ n </BODY> </HTML> ");
}

Public void doPost (HttpServletRequest request,
HttpServletResponse response)
Throws ServletException, IOException {
DoGet (request, response );
}
}

Related Article

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.