Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
94
Chapter 4 Handling the Client Request: HTTP Request Headers
matically: the servlet needs to explicitly read the request headers to make
use of this information.
GET /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.7 [en] (Win98; U)
4.1 Reading Request Headers from
Servlets
Reading headers is straightforward; just call the
getHeader
method of
HttpServletRequest
, which returns a
String
if the specified header was
supplied on this request,
null
otherwise. Header names are not case sensi
tive. So, for example,
request.getHeader("Connection")
and
request.getHeader("connection")
are interchangeable.
Although
getHeader
is the general purpose way to read incoming head
ers, there are a couple of headers that are so commonly used that they have
special access methods in
HttpServletRequest
. I'll list them here, and
remember that Appendix A (Servlet and JSP Quick Reference) gives a sepa
rate syntax summary.
getCookies
The
getCookies
method returns the contents of the
Cookie
header, parsed and stored in an array of
Cookie
objects. This
method is discussed more in Chapter 8 (Handling Cookies).
getAuthType
and
getRemoteUser
The
getAuthType
and
getRemoteUser
methods break the
Authorization
header into its component pieces. Use of the
Authorization
header is illustrated in Section 4.5 (Restricting
Access to Web Pages).
getContentLength
The
getContentLength
method returns the value of the
Content Length
header (as an
int
).
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved