Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
5.1 Servlet Equivalent of CGI Variables
117
HTTP_XXX_YYY
Variables of the form
HTTP_HEADER_NAME
were how CGI programs
obtained access to arbitrary HTTP request headers. The
Cookie
header
became
HTTP_COOKIE
,
User Agent
became
HTTP_USER_AGENT
,
Ref
erer
became
HTTP_REFERER
, and so forth. Servlets should just use
request.getHeader
or one of the shortcut methods described in
Chapter 4 (Handling the Client Request: HTTP Request Headers).
PATH_INFO
This variable supplies any path information attached to the URL after the
address of the servlet but before the query data. For example, with
http://host/servlet/coreservlets.SomeServ
let/foo/bar?baz=quux
, the path information is
/foo/bar
. Since serv
lets, unlike standard CGI programs, can talk directly to the server, they
don't need to treat path information specially. Path information could be
sent as part of the regular form data and then translated by
getServlet
Context().getRealPath
. Access the value of
PATH_INFO
by using
request.getPathInfo()
.
PATH_TRANSLATED
PATH_TRANSLATED
gives the path information mapped to a real path on
the server. Again, with servlets there is no need to have a special case for
path information, since a servlet can call
getServletContext().get
RealPath
to translate partial URLs into real paths. This translation is
not possible with standard CGI because the CGI program runs entirely
separately from the server. Access this variable by means of
request.getPathTranslated()
.
QUERY_STRING
For
GET
requests, this variable gives the attached data as a single string
with values still URL encoded. You rarely want the raw data in servlets;
instead, use
request.getParameter
to access individual parameters,
as described in Chapter 3 (Handling the Client Request: Form Data).
However, if you do want the raw data, you can get it via
request.getQueryString()
.
REMOTE_ADDR
This variable designates the IP address of the client that made the
request, as a
String
(e.g.,
"198.137.241.30"
). Access it by calling
request.getRemoteAddr()
.
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