Chapter 5. Handling the Client Request: HTTP Request Headers
Topics in This Chapter
One of the keys to creating effective servlets is understanding how to manipulate the HyperText Transfer Protocol (HTTP). Thoroughly understanding 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 the most important HTTP 1.1 request headers, summarizing how and why they would be used in a servlet. As we see later, request headers are read and applied the same way in JSP pages as they are in servlets.
Note that HTTP request headers are distinct from the form (query) data discussed in the previous chapter. 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 : www.somebookstore.com Referer : http://www.somebookstore.com/findbooks.html User-Agent : Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)