Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 2720 Building Web Applications Servlet – Getting and Setting HTTP Headers.

Similar presentations


Presentation on theme: "CSC 2720 Building Web Applications Servlet – Getting and Setting HTTP Headers."— Presentation transcript:

1 CSC 2720 Building Web Applications Servlet – Getting and Setting HTTP Headers

2 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

3 Introduction  The headers in a HTTP request/response define various characteristics of the data that is requested or the data that has been provided. 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 header section of a HTTP response

4 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

5 Methods for obtaining HTTP Header Fields  Through HttpServletRequest object (request)HttpServletRequest  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

6 Header name Header value(s) <% Enumeration headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String name = (String)headerNames.nextElement(); Enumeration values = request.getHeaders((String)name); out.println(" " + name + " "); 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(" "); } out.println(" "); } %> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 (Part of a JSP file): Dumping all header fields in a HTTP request

7 Header name Header value(s) accept image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x- shockwave-flash, application/vnd.ms-excel, application/vnd.ms- powerpoint, application/msword, application/x-silverlight, */* refererhttp://localhost:8084/csc2720/ accept- language en-us ua-cpux86 accept- encoding gzip, deflate user-agent Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1;.NET CLR 1.1.4322;.NET CLR 2.0.50727) hostlocalhost:8084 connectionKeep-Alive cookieJSESSIONID=E4BEE528F568A68443E26230BDF54767 Sample output produced by the previous segment of JSP code.

8 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

9 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.

10 Methods for setting HTTP Header  Through HttpServletResponse object (response)HttpServletResponse  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.

11 Methods for setting Commonly Used Headers  Through HttpServletResponse object (response)HttpServletResponse  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.

12 References  Wiki: List of HTTP headers  http://en.wikipedia.org/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 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html


Download ppt "CSC 2720 Building Web Applications Servlet – Getting and Setting HTTP Headers."

Similar presentations


Ads by Google