Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

2.5 The Client Request: HTTP Request Headers

One of the keys to creating effective servlets is understanding how to manipulate the HyperText Transfer Protocol (HTTP). Getting a thorough grasp of this protocol is not an esoteric, theoretical concept, but rather a practical issue that can have an immediate impact on the performance and usability of your servlets. This section discusses the HTTP information that is sent from the browser to the server in the form of request headers. It explains a few of the most important HTTP 1.1 request headers, summarizing how and why they would be used in a servlet. For more details and examples, see Chapter 4 of Core Servlets and JavaServer Pages (available in PDF at http://www.moreservlets.com).

Note that HTTP request headers are distinct from the form (query) data discussed in the previous section. Form data results directly from user input and is sent as part of the URL for GET requests and on a separate line for POST requests. Request headers, on the other hand, are indirectly set by the browser and are sent immediately following the initial GET or POST request line. For instance, the following example shows an HTTP request that might result from a user submitting a book-search request to a servlet at http://www.somebookstore.com/servlet/Search. The request includes the headers Accept, Accept-Encoding, Connection, Cookie, Host, Referer, and User-Agent, all of which might be important to the operation of the servlet, but none of which can be derived from the form data or deduced automatically: the servlet needs to explicitly read the request headers to make use of this information.

GET /servlet/Search?keywords=servlets+jsp HTTP/1.1
Accept: image/gif, image/jpg, */*
Accept-Encoding: gzip
Connection: Keep-Alive
Cookie: userID=id456578
Host: http://www.somebookstore.com
Referer: http://www.somebookstore.com/findbooks.html
User-Agent: Mozilla/4.7 [en] (Win98; U)

Reading Request Headers from Servlets

Reading headers is straightforward; just call the getHeader method of HttpServletRequest, which returns a String if the specified header was supplied on this request, null otherwise. Header names are not case sensitive. So, for example, request.getHeader("Connection") is interchangeable with request.getHeader("connection").

Although getHeader is the general-purpose way to read incoming headers, a few headers are so commonly used that they have special access methods in HttpServletRequest. Following is a summary.

  • getCookies The getCookies method returns the contents of the Cookie header, parsed and stored in an array of Cookie objects. This method is discussed in more detail in Section 2.9 (Cookies).

  • getAuthType and getRemoteUser The getAuthType and getRemoteUser methods break the Authorization header into its component pieces.

  • getContentLength The getContentLength method returns the value of the Content-Length header (as an int).

  • getContentType The getContentType method returns the value of the Content-Type header (as a String).

  • getDateHeader and getIntHeader The getDateHeader and getIntHeader methods read the specified headers and then convert them to Date and int values, respectively.

  • getHeaderNames Rather than looking up one particular header, you can use the getHeaderNames method to get an Enumeration of all header names received on this particular request. This capability is illustrated in Listing 2.11.

  • getHeaders In most cases, each header name appears only once in the request. Occasionally, however, a header can appear multiple times, with each occurrence listing a separate value. Accept-Language is one such example. You can use getHeaders to obtain an Enumeration of the values of all occurrences of the header.

Finally, in addition to looking up the request headers, you can get information on the main request line itself, also by means of methods in HttpServletRequest. Here is a summary of the three main methods.

  • getMethod The getMethod method returns the main request method (normally GET or POST, but methods like HEAD, PUT, and DELETE are possible).

  • getRequestURI The getRequestURI method returns the part of the URL that comes after the host and port but before the form data. For example, for a URL of http://randomhost.com/servlet/search.BookSearch, getRequestURI would return "/servlet/search.BookSearch".

  • getProtocol The getProtocol method returns the third part of the request line, which is generally HTTP/1.0 or HTTP/1.1. Servlets should usually check getProtocol before specifying response headers (Section 2.8) that are specific to HTTP 1.1.

Example: Making a Table of All Request Headers

Listing 2.11 shows a servlet that simply creates a table of all the headers it receives, along with their associated values. It also prints out the three components of the main request line (method, URI, and protocol). Figures 2–8 and 2–9 show typical results with Netscape and Internet Explorer.

Listing 2.11 ShowRequestHeaders.java

package moreservlets;

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

/** Shows all the request headers sent on this request. */

public class ShowRequestHeaders extends HttpServlet {
 public void doGet(HttpServletRequest request,
          HttpServletResponse response)
   throws ServletException, IOException {
  PrintWriter out = response.getWriter();
  String title = "Servlet Example: Showing 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><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));

Figure 2-8Figure 2–8 Request headers sent by Netscape 4.7 on Windows 98.

Figure 2-9Figure 2–9 Request headers sent by Internet Explorer 5.0 on Windows 98.

Understanding HTTP 1.1 Request Headers

Access to the request headers permits servlets to perform a number of optimizations and to provide a number of features not otherwise possible. This subsection summarizes the headers most often used by servlets; more details are given in Core Servlets and JavaServer Pages, Chapter 4 (in PDF at http://www.moreservlets.com). Note that HTTP 1.1 supports a superset of the headers permitted in HTTP 1.0. For additional details on these and other headers, see the HTTP 1.1 specification, given in RFC 2616. The official RFCs are archived in a number of places; your best bet is to start at http://www.rfc-editor.org/ to get a current list of the archive sites.


This header specifies the MIME types that the browser or other clients can handle. A servlet that can return a resource in more than one format can examine the Accept header to decide which format to use. For example, images in PNG format have some compression advantages over those in GIF, but only a few browsers support PNG. If you had images in both formats, a servlet could call request.getHeader("Accept"), check for image/png, and if it finds a match, use xxx.png filenames in all the IMG elements it generates. Otherwise, it would just use xxx.gif.

See Table 2.1 in Section 2.8 (The Server Response: HTTP Response Headers) for the names and meanings of the common MIME types

Note that Internet Explorer 5 has a bug whereby the Accept header is not sent properly when you reload a page. It is sent properly on the original request, however.


This header indicates the character sets (e.g., ISO-8859-1) the browser can use.


This header designates the types of encodings that the client knows how to handle. If the server receives this header, it is free to encode the page by using one of the formats specified (usually to reduce transmission time), sending the Con_tent-Encoding response header to indicate that it has done so. This encoding type is completely distinct from the MIME type of the actual document (as specified in the Content-Type response header), since this encoding is reversed before the browser decides what to do with the content. On the other hand, using an encoding the browser doesn't understand results in totally incomprehensible pages. Consequently, it is critical that you explicitly check the Accept-Encoding header before using any type of content encoding. Values of gzip or compress are the two most common possibilities.

Compressing pages before returning them is a valuable service because the decoding time is likely to be small compared to the savings in transmission time. See Section 9.5 where gzip compression is used to reduce download times by a factor of 10.


This header specifies the client's preferred languages in case the servlet can produce results in more than one language. The value of the header should be one of the standard language codes such as en, en-us, da, etc. See RFC 1766 for details (start at http://www.rfc-editor.org/ to get a current list of the RFC archive sites).


This header is used by clients to identify themselves when accessing password-protected Web pages. For details, see Chapters 7 and 8.


This header indicates whether the client can handle persistent HTTP connections. Persistent connections permit the client or other browser to retrieve multiple files (e.g., an HTML file and several associated images) with a single socket connection, saving the overhead of negotiating several independent connections. With an HTTP 1.1 request, persistent connections are the default, and the client must specify a value of close for this header to use old-style connections. In HTTP 1.0, a value of Keep-Alive means that persistent connections should be used.

Each HTTP request results in a new invocation of a servlet (i.e., a thread calling the servlet's service and doXxx methods), regardless of whether the request is a separate connection. That is, the server invokes the servlet only after the server has already read the HTTP request. This means that servlets need help from the server to handle persistent connections. Consequently, the servlet's job is just to make it possible for the server to use persistent connections, which the servlet does by setting the Content-Length response header.


This header is applicable only to POST requests and gives the size of the POST data in bytes. Rather than calling request.getIntHeader("Con_tent-Length"), you can simply use request.getContentLength(). However, since servlets take care of reading the form data for you (see Section 2.4), you rarely use this header explicitly.


This header is used to return cookies to servers that previously sent them to the browser. Never read this header directly; use request.getCookies instead. For details, see Section 2.9 (Cookies). Technically, Cookie is not part of HTTP 1.1. It was originally a Netscape extension but is now widely supported, including in both Netscape Navigator/Communicator and Microsoft Internet Explorer.


In HTTP 1.1, browsers and other clients are required to specify this header, which indicates the host and port as given in the original URL. Due to request forwarding and machines that have multiple hostnames, it is quite possible that the server could not otherwise determine this information. This header is not new in HTTP 1.1, but in HTTP 1.0 it was optional, not required.


This header indicates that the client wants the page only if it has been changed after the specified date. The server sends a 304 (Not Modified) header if no newer result is available. This option is useful because it lets browsers cache documents and reload them over the network only when they've changed. However, servlets don't need to deal directly with this header. Instead, they should just implement the getLastModified method to have the system handle modification dates automatically. See Section 2.8 of Core Servlets and JavaServer Pages (available in PDF at http://www.moreservlets.com) for an example of the use of getLastModified.


This header is the reverse of If-Modified-Since; it specifies that the operation should succeed only if the document is older than the specified date. Typically, If-Modified-Since is used for GET requests ("give me the document only if it is newer than my cached version"), whereas If-Unmodi_fied-Since is used for PUT requests ("update this document only if nobody else has changed it since I generated it"). This header is new in HTTP 1.1.


This header indicates the URL of the referring Web page. For example, if you are at Web page 1 and click on a link to Web page 2, the URL of Web page 1 is included in the Referer header when the browser requests Web page 2. All major browsers set this header, so it is a useful way of tracking where requests come from. This capability is helpful for tracking advertisers who refer people to your site, for slightly changing content depending on the referring site, or simply for keeping track of where your traffic comes from. In the last case, most people simply rely on Web server log files, since the Referer is typically recorded there. Although the Referer header is useful, don't rely too heavily on it since it can easily be spoofed by a custom client. Finally, note that, due to a spelling mistake by one of the original HTTP authors, this header is Referer, not the expected Referrer.


This header identifies the browser or other client making the request and can be used to return different content to different types of browsers. Be wary of this usage when dealing only with Web browsers; relying on a hard-coded list of browser versions and associated features can make for unreliable and hard-to-modify servlet code. Whenever possible, use something specific in the HTTP headers instead. For example, instead of trying to remember which browsers support gzip on which platforms, simply check the Accept- Encoding header.

However, the User-Agent header is quite useful for distinguishing among different categories of client. For example, Japanese developers might see if the User-Agent is an Imode cell phone (in which case you would redirect to a chtml page), a Skynet cell phone (in which case you would redirect to a wml page), or a Web browser (in which case you would generate regular HTML).

Most Internet Explorer versions list a "Mozilla" (Netscape) version first in their User-Agent line, with the real browser version listed parenthetically. This is done for compatibility with JavaScript, where the User-Agent header is sometimes used to determine which JavaScript features are supported. Also note that this header can be easily spoofed, a fact that calls into question the reliability of sites that use this header to "show" market penetration of various browser versions.

  • + Share This
  • 🔖 Save To Your Account