Js|servlet| Request
5.1 http request headers Overview
An HTTP client, such as a browser, 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.
Here are some of the most common request headers:
Accept: The MIME type acceptable to the browser.
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.
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 headers, see the discussion in the chapter "Cookie Processing" later.
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.
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/.
5.2 Read the request header in the servlet
It is convenient to read HTTP headers in the servlet, just call the HttpServletRequest GetHeader method. If the specified header information is provided in the customer request, GetHeader returns the corresponding string, otherwise, returns NULL. Partial header information is often used, and they have a dedicated access method: The GetCookies method returns the contents of the cookie header, which is parsed and stored in an array of cookie objects, as discussed later in the cookie section ; The Getauthtype and Getremoteuser methods read part of the authorization header respectively; the Getdateheader and Getintheader methods read the specified headers and then return a date or integer value.
In addition to reading the specified headers, you can use Getheadernames to get a enumeration object for all header names in the request.
Finally, in addition to looking at the request header information, we can also get some information from the request main command line. The GetMethod method returns the request method, which is usually a get or post, but may also be head, put, or delete. The Getrequesturi method returns the URI (the part of the URL that precedes the host and port to the form data). Getrequestprotocol returns the third part of the request command, typically "http/1.0" or "http/1.1".
5.3 instance: output all Request headers
The following servlet instance outputs all the received request headers and their values in tabular form. In addition, the servlet outputs three parts of the main request command: The request method, the URI, the 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 (title) + Servletutilities.headwithtitle
"<body bgcolor=\" #FDF5E6 \ ">\n" +
"
"<B> Request Method: </B>" +
Request.getmethod () + "<BR> \ n" +
"<B> Request URI: </B>" +
Request.getrequesturi () + "<BR> \ n" +
"<B> Request Protocol: </B>" +
Request.getprotocol () + "<BR> <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);
}
}