10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

An HTTP request URL contains the following parts:<br />

http://[host]:[port][request-path]?[query-string]<br />

<strong>The</strong> request path is further composed of the following elements:<br />

■ Context path: A concatenation of a forward slash (/) with the context root of the servlet’s<br />

web application.<br />

■ Servlet path: <strong>The</strong> path section that corresponds to the component alias that activated this<br />

request. This path starts with a forward slash (/).<br />

■ Path info: <strong>The</strong> part of the request path that is not part of the context path or the servlet path.<br />

If the context path is /catalog and for the aliases listed in Table 4–4, Table 4–5 gives some<br />

examples of how the URL will be parsed.<br />

TABLE 4–4 Aliases<br />

Pattern Servlet<br />

/lawn/* LawnServlet<br />

/*.jsp JSPServlet<br />

TABLE 4–5 Request Path Elements<br />

Request Path Servlet Path Path Info<br />

/catalog/lawn/index.html /lawn /index.html<br />

/catalog/help/feedback.jsp /help/feedback.jsp null<br />

Query strings are composed of a set of parameters and values. Individual parameters are<br />

retrieved from a request by using the getParameter method. <strong>The</strong>re are two ways to generate<br />

query strings:<br />

■ A query string can explicitly appear in a web page. For example, an HTML page generated<br />

by CatalogServlet could contain the link Add<br />

To Cart. CatalogServlet extracts the parameter named Add as follows:<br />

String bookId = request.getParameter("Add");<br />

Writing Service Methods<br />

■ A query string is appended to a URL when a form with a GET HTTP method is submitted. In<br />

the Duke’s Bookstore application, CashierServlet generates a form, then a user name<br />

input to the form is appended to the URL that maps to ReceiptServlet, and finally<br />

ReceiptServlet extracts the user name using the getParameter method.<br />

Chapter 4 • <strong>Java</strong> ServletTechnology 111

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!