A request contains data passed between a client and the servlet. All requests implement the ServletRequest interface. This interface defines methods for accessing the following information:
Parameters, which are typically used to convey information between clients and servlets
Object-valued attributes, which are typically used to pass information between the servlet container and a servlet or between collaborating servlets
Information about the protocol used to communicate the request and about the client and server involved in the request
Information relevant to localization
For example, in CatalogServlet the identifier of the book that a customer wishes to purchase is included as a parameter to the request. The following code fragment illustrates how to use the getParameter method to extract the identifier:
String bookId = request.getParameter("Add"); if (bookId != null) { Book book = bookDB.getBook(bookId);
You can also retrieve an input stream from the request and manually parse the data. To read character data, use the BufferedReader object returned by the request’s getReader method. To read binary data, use the ServletInputStream returned by getInputStream.
HTTP servlets are passed an HTTP request object, HttpServletRequest, which contains the request URL, HTTP headers, query string, and so on.
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.