csc 2720 building web applications servlet – getting and setting http headers
TRANSCRIPT
![Page 1: CSC 2720 Building Web Applications Servlet – Getting and Setting HTTP Headers](https://reader035.vdocuments.us/reader035/viewer/2022080915/56649d895503460f94a6fb10/html5/thumbnails/1.jpg)
CSC 2720Building Web Applications
Servlet –
Getting and Setting HTTP Headers
![Page 2: CSC 2720 Building Web Applications Servlet – Getting and Setting HTTP Headers](https://reader035.vdocuments.us/reader035/viewer/2022080915/56649d895503460f94a6fb10/html5/thumbnails/2.jpg)
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
![Page 3: CSC 2720 Building Web Applications Servlet – Getting and Setting HTTP Headers](https://reader035.vdocuments.us/reader035/viewer/2022080915/56649d895503460f94a6fb10/html5/thumbnails/3.jpg)
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 OKDate: Mon, 23 May 2005 22:38:34 GMTServer: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)Last-Modified: Wed, 08 Jan 2003 23:11:55 GMTEtag: "3f80f-1b6-3e1cb03b"Accept-Ranges: bytesContent-Length: 438Connection: closeContent-Type: text/html; charset=UTF-8
Body of the contents goes here …
The header section of a HTTP response
![Page 4: CSC 2720 Building Web Applications Servlet – Getting and Setting HTTP Headers](https://reader035.vdocuments.us/reader035/viewer/2022080915/56649d895503460f94a6fb10/html5/thumbnails/4.jpg)
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
![Page 5: CSC 2720 Building Web Applications Servlet – Getting and Setting HTTP Headers](https://reader035.vdocuments.us/reader035/viewer/2022080915/56649d895503460f94a6fb10/html5/thumbnails/5.jpg)
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
![Page 6: CSC 2720 Building Web Applications Servlet – Getting and Setting HTTP Headers](https://reader035.vdocuments.us/reader035/viewer/2022080915/56649d895503460f94a6fb10/html5/thumbnails/6.jpg)
<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>
1234567891011121314151617181920212223 (Part of a JSP file): Dumping all header fields in
a HTTP request
![Page 7: CSC 2720 Building Web Applications Servlet – Getting and Setting HTTP Headers](https://reader035.vdocuments.us/reader035/viewer/2022080915/56649d895503460f94a6fb10/html5/thumbnails/7.jpg)
Header name
Header value(s)
acceptimage/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, */*
referer http://localhost:8084/csc2720/
accept-language
en-us
ua-cpu x86
accept-encoding
gzip, deflate
user-agentMozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
host localhost:8084
connection Keep-Alive
cookie JSESSIONID=E4BEE528F568A68443E26230BDF54767
Sample output produced by the previous segment of JSP code.
![Page 8: CSC 2720 Building Web Applications Servlet – Getting and Setting HTTP Headers](https://reader035.vdocuments.us/reader035/viewer/2022080915/56649d895503460f94a6fb10/html5/thumbnails/8.jpg)
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
![Page 9: CSC 2720 Building Web Applications Servlet – Getting and Setting HTTP Headers](https://reader035.vdocuments.us/reader035/viewer/2022080915/56649d895503460f94a6fb10/html5/thumbnails/9.jpg)
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.
![Page 10: CSC 2720 Building Web Applications Servlet – Getting and Setting HTTP Headers](https://reader035.vdocuments.us/reader035/viewer/2022080915/56649d895503460f94a6fb10/html5/thumbnails/10.jpg)
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.
![Page 11: CSC 2720 Building Web Applications Servlet – Getting and Setting HTTP Headers](https://reader035.vdocuments.us/reader035/viewer/2022080915/56649d895503460f94a6fb10/html5/thumbnails/11.jpg)
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.
![Page 12: CSC 2720 Building Web Applications Servlet – Getting and Setting HTTP Headers](https://reader035.vdocuments.us/reader035/viewer/2022080915/56649d895503460f94a6fb10/html5/thumbnails/12.jpg)
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