15.5 Applying MVC: Bank Account Balances

15.5 Applying MVC: Bank Account Balances

In this section, we apply the MVC approach to an application that displays bank account balances. The controller servlet (Listing 15.2) reads a customer ID and passes that to some data-access code that returns a BankCustomer value bean (Listing 15.3). The servlet then stores the bean in the HttpServletRequest object where it will be accessible from destination JSP pages but nowhere else. If the account balance of the resulting customer is negative, the servlet forwards to a page designed for delinquent customers (Listing 15.4, Figure 15-2). If the customer has a positive balance of less than $10,000, the servlet transfers to the standard balance-display page (Listing 15.5, Figure 15-3). Next, if the customer has a balance of $10,000 or more, the servlet forwards the request to a page reserved for elite customers (Listing 15.6, Figure 15-4). Finally, if the customer ID is unrecognized, an error page is displayed (Listing 15.7, Figure 15-5).

Listing 15.2 ShowBalance.java
 package coreservlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** Servlet that reads a customer ID and displays  *  information on the account balance of the customer  *  who has that ID.  */ public class ShowBalance extends HttpServlet {   public void doGet(HttpServletRequest request,                     HttpServletResponse response)       throws ServletException, IOException {  BankCustomer customer =   BankCustomer.getCustomer(request.getParameter("id"));  String address;     if (customer == null) {       address = "/WEB-INF/bank-account/UnknownCustomer.jsp";     } else if (customer.getBalance() < 0) {       address = "/WEB-INF/bank-account/NegativeBalance.jsp";  request.setAttribute("badCustomer", customer);  } else if (customer.getBalance() < 10000) {       address = "/WEB-INF/bank-account/NormalBalance.jsp";  request.setAttribute("regularCustomer", customer);  } else {       address = "/WEB-INF/bank-account/HighBalance.jsp";  request.setAttribute("eliteCustomer", customer);  }  RequestDispatcher dispatcher =   request.getRequestDispatcher(address);   dispatcher.forward(request, response);  } } 
Listing 15.3 BankCustomer.java
 package coreservlets; import java.util.*; /** Bean to represent a bank customer. */ public class BankCustomer {   private String id, firstName, lastName;   private double balance;   public BankCustomer(String id,                       String firstName,                       String lastName,                       double balance) {     this.id = id;     this.firstName = firstName;     this.lastName = lastName;     this.balance = balance;   }   public String getId() {     return(id);   }   public String getFirstName() {     return(firstName);   }   public String getLastName() {     return(lastName);   }   public double getBalance() {     return(balance);   }   public double getBalanceNoSign() {     return(Math.abs(balance));   }   public void setBalance(double balance) {     this.balance = balance;   }   // Makes a small table of banking customers.   private static HashMap customers;   static {     customers = new HashMap();     customers.put("id001",                   new BankCustomer("id001",                                    "John",                                    "Hacker",                                    -3456.78));     customers.put("id002",                   new BankCustomer("id002",                                    "Jane",                                    "Hacker",                                    1234.56));     customers.put("id003",                   new BankCustomer("id003",                                    "Juan",                                    "Hacker",                                    987654.32));   }   /** Finds the customer with the given ID.    *  Returns null if there is no match.    */   public static BankCustomer getCustomer(String id) {     return((BankCustomer)customers.get(id));   } } 
Listing 15.4 NegativeBalance.jsp
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>You Owe Us Money!</TITLE> <LINK REL=STYLESHEET       HREF="/bank-support/JSP-Styles.css"       TYPE="text/css"> </HEAD> <BODY> <TABLE BORDER=5 ALIGN="CENTER">   <TR><TH CLASS="TITLE">       We Know Where You Live!</TABLE> <P> <IMG SRC="/bank-support/Club.gif" ALIGN="LEFT">  <jsp:useBean id="badCustomer"   type="coreservlets.BankCustomer"   scope="request" />  Watch out,  <jsp:getProperty name="badCustomer" property="firstName" />  , we know where you live. <P> Pay us the $  <jsp:getProperty name="badCustomer" property="balanceNoSign" />  you owe us before it is too late! </BODY></HTML> 
Listing 15.5 NormalBalance.jsp
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Your Balance</TITLE> <LINK REL=STYLESHEET       HREF="/bank-support/JSP-Styles.css"       TYPE="text/css"> </HEAD> <BODY> <TABLE BORDER=5 ALIGN="CENTER">   <TR><TH CLASS="TITLE">       Your Balance</TABLE> <P> <IMG SRC="/bank-support/Money.gif" ALIGN="RIGHT">  <jsp:useBean id="regularCustomer"   type="coreservlets.BankCustomer"   scope="request" />  <UL>   <LI>First name:  <jsp:getProperty name="regularCustomer"   property="firstName" />  <LI>Last name:  <jsp:getProperty name="regularCustomer"   property="lastName" />  <LI>ID:  <jsp:getProperty name="regularCustomer"   property="id" />  <LI>Balance: $  <jsp:getProperty name="regularCustomer"   property="balance" />  </UL> </BODY></HTML> 
Listing 15.6 HighBalance.jsp
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Your Balance</TITLE> <LINK REL=STYLESHEET       HREF="/bank-support/JSP-Styles.css"       TYPE="text/css"> </HEAD> <BODY> <TABLE BORDER=5 ALIGN="CENTER">   <TR><TH CLASS="TITLE">       Your Balance</TABLE> <P> <CENTER><IMG SRC="/bank-support/Sailing.gif"></CENTER> <BR CLEAR="ALL">  <jsp:useBean id="eliteCustomer"   type="coreservlets.BankCustomer"   scope="request" />  It is an honor to serve you,  <jsp:getProperty name="eliteCustomer" property="firstName" />   <jsp:getProperty name="eliteCustomer" property="lastName" />  ! <P> Since you are one of our most valued customers, we would like to offer you the opportunity to spend a mere fraction of your $  <jsp:getProperty name="eliteCustomer"  property="balance" />  on a boat worthy of your status. Please visit our boat store for more information. </BODY></HTML> 
Listing 15.7 UnknownCustomer.jsp
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Unknown Customer</TITLE> <LINK REL=STYLESHEET       HREF="/bank-support/JSP-Styles.css"       TYPE="text/css"> </HEAD> <BODY> <TABLE BORDER=5 ALIGN="CENTER">   <TR><TH CLASS="TITLE">       Unknown Customer</TABLE> <P> Unrecognized customer ID. </BODY></HTML> 
Figure 15-2. The ShowCustomer servlet with an ID corresponding to a customer with a negative balance.

graphics/15fig02.jpg

Figure 15-3. The ShowCustomer servlet with an ID corresponding to a customer with a normal balance.

graphics/15fig03.jpg

Figure 15-4. The ShowCustomer servlet with an ID corresponding to a customer with a high balance.

graphics/15fig04.jpg

Figure 15-5. The ShowCustomer servlet with an unknown customer ID.

graphics/15fig05.jpg



Core Servlets and JavaServer Pages (Vol. 1.Core Technologies)
Core Servlets and Javaserver Pages: Core Technologies, Vol. 1 (2nd Edition)
ISBN: 0130092290
EAN: 2147483647
Year: 2002
Pages: 194

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net