background image

Context Path and Servlet Path

<< Writing Service Methods | Constructing Responses >>
<< Writing Service Methods | Constructing Responses >>

Context Path and Servlet Path

An HTTP request URL contains the following parts:
http://[
host]:[port][request-path]?[query-string]
The request path is further composed of the following elements:
Context path
: A concatenation of a forward slash (/) with the context root of the servlet's
web application.
Servlet path
: The path section that corresponds to the component alias that activated this
request. This path starts with a forward slash (/).
Path info
: The part of the request path that is not part of the context path or the servlet path.
If the context path is /catalog and for the aliases listed in
Table 4­4
,
Table 4­5
gives some
examples of how the URL will be parsed.
TABLE 4­4
Aliases
Pattern
Servlet
/lawn/*
LawnServlet
/*.jsp
JSPServlet
TABLE 4­5
Request Path Elements
Request Path
Servlet Path
Path Info
/catalog/lawn/index.html
/lawn
/index.html
/catalog/help/feedback.jsp
/help/feedback.jsp
null
Query strings are composed of a set of parameters and values. Individual parameters are
retrieved from a request by using the getParameter method. There are two ways to generate
query strings:
A query string can explicitly appear in a web page. For example, an HTML page generated
by CatalogServlet could contain the link <a href="/bookstore1/catalog?Add=101">Add
To Cart</a>
. CatalogServlet extracts the parameter named Add as follows:
String bookId = request.getParameter(
"Add");
A query string is appended to a URL when a form with a GET HTTP method is submitted. In
the Duke's Bookstore application, CashierServlet generates a form, then a user name
input to the form is appended to the URL that maps to ReceiptServlet, and finally
ReceiptServlet
extracts the user name using the getParameter method.
Writing Service Methods
Chapter 4 · Java Servlet Technology
111