Listing 15 - Listing 15.1 Request Forwarding Example public...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Listing 15.1 Request Forwarding Example public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String operation = request.getParameter("operation"); if (operation == null) { operation = "unknown"; } String address; if (operation.equals("order")) { address = "/WEB-INF/Order.jsp"; } else if (operation.equals("cancel")) { address = "/WEB-INF/Cancel.jsp"; } else { address = "/WEB-INF/UnknownOperation.jsp"; } RequestDispatcher dispatcher = request.getRequestDispatcher(address); dispatcher.forward(request, response); } 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) {
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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);
Background image of page 2
} 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;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/14/2011 for the course MATH 401 taught by Professor H.ayad during the Spring '11 term at Cairo University.

Page1 / 12

Listing 15 - Listing 15.1 Request Forwarding Example public...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online