Java Servlets

MVC Example

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 {

    //Fill up the bean
    BankCustomer customer = BankCustomer.getCustomer(request.getParameter("id"));

    String address;

    //Use a different view depending on the customer.
    //
    if (customer == null) {
      address = "/bank-account/UnknownCustomer.jsp";
    } else if (customer.getBalance() < 0) {
      address = "/bank-account/NegativeBalance.jsp";
      request.setAttribute("badCustomer", customer);
    } else if (customer.getBalance() < 10000) {
      address = "/bank-account/NormalBalance.jsp";
      request.setAttribute("regularCustomer", customer);
    } else {
      address = "/bank-account/HighBalance.jsp";
      request.setAttribute("eliteCustomer", customer);
    }

    //Forward control to the .jsp page
    RequestDispatcher dispatcher = request.getRequestDispatcher(address);
    dispatcher.forward(request, response);
  }
}
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. for Testing purposes.
  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));
  }
}

<!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>
     <H1>Your Balance</H1>
    <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>

José M. Vidal .

81 of 89