package coreservlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; /** Base class for pages showing catalog entries. * Servlets that extend this class must specify * the catalog entries that they are selling and the page * title before the servlet is ever accessed. This * is done by putting calls to setItems and setTitle * in init. *

* Taken from Core Servlets and JavaServer Pages * from Prentice Hall and Sun Microsystems Press, * http://www.coreservlets.com/. * © 2000 Marty Hall; may be freely used or adapted. */ public abstract class CatalogPage extends HttpServlet { private Item[] items; private String[] itemIDs; private String title; /** Given an array of item IDs, look them up in the * Catalog and put their corresponding Item entry * into the items array. The Item contains a short * description, a long description, and a price, * using the item ID as the unique key. *

* Servlets that extend CatalogPage must call * this method (usually from init) before the servlet * is accessed. */ protected void setItems(String[] itemIDs) { this.itemIDs = itemIDs; items = new Item[itemIDs.length]; for(int i=0; i * Servlets that extend CatalogPage must call * this method (usually from init) before the servlet * is accessed. */ protected void setTitle(String title) { this.title = title; } /** First display title, then, for each catalog item, * put its short description in a level-two (H2) heading * with the price in parentheses and long description * below. Below each entry, put an order button * that submits info to the OrderPage servlet for * the associated catalog entry. *

* To see the HTML that results from this method, do * "View Source" on KidsBooksPage or TechBooksPage, two * concrete classes that extend this abstract class. */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); if (items == null) { response.sendError(response.SC_NOT_FOUND, "Missing Items."); return; } PrintWriter out = response.getWriter(); out.println(ServletUtilities.headWithTitle(title) + "\n" + "

" + title + "

"); Item item; for(int i=0; i"); item = items[i]; // Show error message if subclass lists item ID // that's not in the catalog. if (item == null) { out.println("" + "Unknown item ID " + itemIDs[i] + ""); } else { out.println(); String formURL = "/servlet/coreservlets.OrderPage"; // Pass URLs that reference own site through encodeURL. formURL = response.encodeURL(formURL); out.println ("
\n" + "\n" + "

" + item.getShortDescription() + " ($" + item.getCost() + ")

\n" + item.getLongDescription() + "\n" + "

\n

\n" + "\n" + "
\n

\n

"); } } out.println("
\n"); } /** POST and GET requests handled identically. */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }