Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
15.2 Example: An On Line Travel Agent
359
tially based upon the action requested. For example, pressing the Book
Flights button should show a list of available flights on the dates specified,
ordered by price (see Figure 15 1). The user's real name, frequent flyer infor
mation, and credit card number should be used to generate the page. On the
other hand, selecting Edit Account should show any previously entered cus
tomer information, letting the user modify values or add entries. Likewise, the
actions resulting from choosing Rent Cars or Find Hotels will share much
of the same customer data but will have a totally different presentation.
To accomplish the desired behavior, the front end (Listing 15.2) submits
the request to the top level travel servlet shown in Listing 15.3. This servlet
looks up the customer information (see Listings 15.5 through 15.9), puts it in
the
HttpSession
object associating the value (of type
coreservlets.Trav
elCustomer
) with the name
customer
, and then forwards the request to a
different JSP page corresponding to each of the possible actions. The desti
nation page (see Listing 15.4 and the result in Figure 15 2) looks up the cus
tomer information by means of
class="coreservlets.TravelCustomer"
scope="session" />
then uses
jsp:getProperty
to insert customer information into various
parts of the page. You should note two things about the
TravelCustomer
class (Listing 15.5).
First, the class spends a considerable amount of effort making the customer
information accessible as plain strings or even HTML formatted strings through
simple properties. Almost every task that requires any substantial amount of
programming at all is spun off into the bean, rather than being performed in the
JSP page itself. This is typical of servlet/JSP integration the use of JSP does not
entirely obviate the need to format data as strings or HTML in Java code. Sig
nificant up front effort to make the data conveniently available to JSP more than
pays for itself when multiple JSP pages access the same type of data.
Second, remember that many servers that automatically reload servlets when
their class files change do not allow bean classes used by JSP to be in
the auto reloading directories. Thus, with the Java Web Server for example,
TravelCustomer
and its supporting classes must be in
install_dir/
classes/coreservlets/
, not
install_dir/servlets/coreservlets/
.
Tomcat 3.0 and the JSWDK 1.0.1 do not support auto reloading servlets, so
TravelCustomer
can be installed in the normal location.
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