Source: LoveJSP. site
Introduction to Request Header
When a client (usually a browser) sends a request to the Web server, it sends a request command line, usually the GET or POST command. When the POST command is sent, it must also send a Request Header called "Content-Length" to the server to specify the Length of the Request data. Besides Content-Length, it can also send other Headers to the server, such:
Acceptable MIME type of the Accept Browser
Character encoding supported by the Accept-Charset Browser
The Accept-Encoding browser knows how to decode the data Encoding type (such as gzip ). Servlets can check whether the browser supports gzip in advance, return the gzipped HTML page to the browser that supports gzip, and set the Content-Encoding response header (response header) indicates that the sent content has been gzipped. In most cases, this can speed up web page download.
The Language specified by the Accept-Language browser. It works when the Server supports multiple languages.
Authorization authentication information, usually the response to the WWW-Authenticate header sent by the server.
Whether to use persistent Connection. If the servlet finds that the value of this field is Keep-Alive, or the browser supports HTTP 1.1 by sending the requested command line (continuous connection is its default option ), persistent connections can reduce the download time of pages that protect many small files.
Content-Length (the number of bytes that pass data when submitted using the POST method)
Cookie (a very important Header used for Cookie-related operations. The detailed information will be described in the following tutorial)
Host (Host and port)
If-Modified-Since (only documents newer than the specified date will be returned. If Not, 304 "Not Modified" will be returned ")
Referer (URL)
User-Agent (client type, generally used to differentiate different browsers)
To learn more about the Request Header, visit the W3C website.
Read Request Header content in Servlet
Reading the value of the Request Header in Servlet is very simple. You only need to call the getHeader method of HttpServletRequest. When you specify the name of the Header to be returned, this method returns the content of the String type Header. If the specified Header does not exist, null is returned. Call getHeaderNames to return Enumeration containing the names of all headers.
Servlet program that reads all Request Header values
The following is an example of the Request Header in the Servlet example in Tomcat. Download this program.
Import java. io .*;
Import java. util .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Public class RequestHeaderExample extends HttpServlet {
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws IOException, ServletException
{
Response. setContentType ("text/html ");
PrintWriter out = response. getWriter ();
Enumeration e = request. getHeaderNames ();
While (e. hasMoreElements ()){
String name = (String) e. nextElement ();
String value = request. getHeader (name );
Out. println (name + "=" + value );
}
}
}