120 likes | 245 Views
CSC 2720 Building Web Applications. Servlet – Getting and Setting HTTP Headers. Outline. What kinds of data are embedded in the HTTP request/response headers? How useful could these data be? What can we achieve by setting HTTP response header?
E N D
CSC 2720Building Web Applications Servlet – Getting and Setting HTTP Headers
Outline • What kinds of data are embedded in the HTTP request/response headers? • How useful could these data be? • What can we achieve by setting HTTP response header? • Java APIs for getting headers from HTTP request • Java APIs for setting HTTP response headers
Introduction HTTP/1.1 200 OK Date: Mon, 23 May 2005 22:38:34 GMT Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux) Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT Etag: "3f80f-1b6-3e1cb03b" Accept-Ranges: bytes Content-Length: 438 Connection: close Content-Type: text/html; charset=UTF-8 Body of the contents goes here … • The headers in a HTTP request/response define various characteristics of the data that is requested or the data that has been provided. The header section of a HTTP response
HTTP Request Headers • You can find out more about your client. • For examples • accept, accept-encoding, accept-language, accept-charset: Content types, compression schemes, languages, and character sets that the client's browser accepts. • user-agent: Info about the client's browser and operating system • referer: The URL of the webpage that "brings" the client to the requested page • cookie: Cookies
Methods for obtaining HTTP Header Fields • Through HttpServletRequest object (request) • java.util.Enumeration getHeaderNames() Get all header names • String getHeader(String name) • long getDateHeader(String name) • int getIntHeader(String name) • Get the value of a specified header as a string / date / integer • java.util.Enumeration getHeaders(String name) Get the values of a specified header as an enumeration of strings • Cookie[] getCookies() Get all cookies
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <table border="1" cellspacing="0" cellpadding="5"> <tr><th>Header name</th><th>Header value(s)</th></tr> <% Enumeration headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String name = (String)headerNames.nextElement(); Enumeration values = request.getHeaders((String)name); out.println("<tr><td>" + name + "</td><td>"); while (values.hasMoreElements()) { out.println(values.nextElement()); // A header name may appear multiple times in the // header if it has multiple values. In such case, // we separate the values by an empty line. if (values.hasMoreElements()) out.println("<p></p>"); } out.println("</td></tr>"); } %> </table> (Part of a JSP file): Dumping all header fields in a HTTP request
Examples of HTTP 1.1 Response Headers • Cache-Control • Tells all caching mechanisms from server to client whether they may cache this object. • To tells a client not to cache the requested resource, set the its value to no-cache. • Content-Language • The language the content is in • Content-Type • The MIME type of the content being returned • Use setContentType to set this header
Examples of HTTP 1.1 Response Headers • Expires • The time at which document should be considered as out-of-date • Last-Modified • The time in which the requested resource was last modified. • Location • To redirect the client's browser to a new URL • Use sendRedirect to set this value • Set-Cookie • The cookies that browser should remember. • Use addCookie to add cookies.
Methods for setting HTTP Header • Through HttpServletResponse object (response) • void setHeader(String name, String value) • void setDateHeader(String name, long value) • void setIntHeader(String name, int value) Set the value of a specific header as string / date / integer. • void addHeader(String name, String value) • void addDateHeader(String name, long value) • void addIntHeader(String name, int value) Add additional values instead of replacing the existing one for a specific header as string / date / integer. • boolean containHeader(String name) Returns true if the named header has already been set.
Methods for setting Commonly Used Headers • Through HttpServletResponse object (response) • void setContentType(String mime) Sets the content type of the response being sent to the client. The content type may include the type of character encoding used, for example, response.setContentType( "text/html; charset=ISO-8859-4"); • void addCookie(Cookie cookie) Adds the specified cookie to the response. • void sendRedirect(String url) Request the client to load the specified URL.
References • Wiki: List of HTTP headers • http://en.wikipedia.org/wiki/List_of_HTTP_headers • HTTP/1.1: Header Field Definitions • http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html