CS6320:  SW Engineering of Web Based Systems

 

HTTP Request Headers
(for HTTP 1.1)


Request Headers are sent by the browser immediately following the initial GET or POST line.   These headers can direct the Servlet how to react. 
  • Servlet needs to explicity reat the request headers.
  • Example of information set in a GET HTTP request (from your Core book) from submitting a book-search at http://www.somebookstore.com/findbooks.html  which invokes the servket at http://www.somebookstore.com/search:
    •  

      GET    /search?keywords=servlets+jsp HTTP/1.1
      Accept:  image/gif, image/jpg, */*
      Accept-Encoding:  gzip
      Connection:  Keep-Alive
      Cookie:   userID=id45678
      Host:   www.somebookstore.com
      Referer: http://www.somebookstore.com/findbooks.html
      User-Agent:  Mozilla/4.7 [en]  (Win98; U)
  • Click here to find out about Methods of HttpServletRequest that are useful to get general information about the HTTP request.
       
How to get Headers:
 
  • call getHeader() method of HttpServletRequest passing the name of the Header you wish to retrieve.  Will return the value as a String.
    • request.getHeader("Connection");
  • For the more commonly used headers there are methods you can use to access the values:

  •  
    getCookies returns an array of Cookies pased
    getAuthType breaks the Authorization header into its component pieces.
    getContentLength returns the value of Content-Length as an int
    getContentType returns value as a String (e.g. "text/html")
    getDateHeader returns value as an instance of the java class Date
    getIntHeader returns value as an int
    getHeaderNames returns as an instance of Enumeration class all of the header names received by this request.
    getHeaders use this if there may be more than one value associated with a header (this is Servlet version sensitive...see API)
Header Meaning
Accept Specifies MIME types that the browser can handle (e.g. image/jpg).   Servlet can look at this to decide what kind of information it can return in its response.
Accept-Charset This tells the Servlet what the character set that can be accepted by the client's browser
Accept-Encoding Indicates the encoding types the client can handle.  If the Servlet decides to return data using an encoding type, it must send an Content-Encoding response header.   Note: this is different than the Content-Type response header...which indicates the kind of data in the document.  The Content-Encoding type is the encoding applied to the content (of type Content-Type) after the content is created.  On the client browser, the recieved data is first decoded before it uses the Content-Type to know how to view the decoded data.

Typically used for compressing data.

Accept-Language Specifies client's preferred languages.  See RFC 1766 for name of languuages (e.g. en-us = U.S. english,  en = British english, etc).
Authorization Used by clients to identify themselves when accessing password-protected Web-pages
Cache-Control Client uses to specify how pages should be cached by proxy servers.
Connection Client uses to indicate if can handle persistent HTTP connections.  Such a connection allows client browser to retrieve multiple files (images, html, etc.) with a single socket connection, saving the overhead of negotiating independent connections for each.
values:
  • keep-alive =  means persistent connection allowable
  • close = means persistent connection NOT allowable


HTTP1.1:  persistent connections are the default. 

Content-Length Gives the size of a POST method request in bytes.
Content-Type Usually used in a HTTP response sent from the Servlet.  Can be given by client to Servlet when the client attaches a document as POST data.
Cookie Client uses this to return cookies to the server.
From Can be used to send as a value the email address of the requestor.  Not sent by browsers, but, other applications sending HTTP requests may use this.
Host Client sends the host and port as given in the URL. 

HTTP 1.1: Browsers are required to send this information.

If-Modified-Since Client sends to indicate they want the requested page (in our case generated by the Servlet), ONLY if it has been changed since the date specified (or in the case of Servlets, if the data used to construct the web-page is newer than this date)
If-Unmodified-Since Opposite of If-Modified-Since
Referer Indicates the URL of referring web-page.
User-Agent Identifies client browser (or application) that made the request.
Can use this when want to know what browser or version of it the client has.  Be careful on how to parse results.
SEE HTTP specs for other HTTP Request headers.

Exercise

© Lynne Grewe