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
* 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) +
"" + title + "
");
Item item;
for(int i=0; i
\n