Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
358
Chapter 15 Integrating Servlets and JSP
The same approach is required for addresses used in
and
HREF=...>
.
Alternative Means of Getting a
RequestDispatcher
In servers that support version 2.2 of the servlet specification, there are
two additional ways of obtaining a
RequestDispatcher
besides the
get
RequestDispatcher
method of
ServletContext
.
First, since most servers let you give explicit names to servlets or JSP
pages, it makes sense to access them by name rather than by path. Use the
getNamedDispatcher
method of
ServletContext
for this task.
Second, you might want to access a resource by a path relative to the cur
rent servlet's location, rather than relative to the server root. This approach is
not common when servlets are accessed in the standard manner
(
http://host/servlet/ServletName
), because JSP files would not be
accessible by means of
http://host/servlet/...
since that URL is
reserved especially for servlets. However, it is common to register servlets
under another path, and in such a case you can use the
getRequest
Dispatcher
method of
HttpServletRequest
rather than the one from
ServletContext
. For example, if the originating servlet is at
http://host/travel/TopLevel
,
getServletContext().getRequestDispatcher("/travel/cruises.jsp")
could be replaced by
request.getRequestDispatcher("cruises.jsp");
15.2 Example: An On Line Travel
Agent
Consider the case of an on line travel agent that has a quick search page, as
shown in Figure 15 1 and Listing 15.2. Users need to enter their e mail
address and password to associate the request with their previously established
customer account. Each request also includes a trip origin, trip destination,
start date, and end date. However, the action that will result will vary substan
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