6.5. The Purchase Display Page

Section 9.4 of Core Servlets and JavaServer Pages gives detailed code for creating and using a shopping cart. (Remember that Core Servlets and JavaServer Pages is available in its entirety in PDF at http://www.moreservlets.com.) Here, I use a much more simplified “cart” to illustrate how session tracking fits in with Web applications. When a user presses the Submit Order button from one of the item display pages of the previous subsection, the item number is sent to the ShowPurchases servlet of Listing 6.16 (by means of the registered name of DisplayPurchases). This servlet looks up the item associated with the item number, puts the item into an ItemList (Listing 6.17), and forwards the request to the sucker.jsp page (Listing 6.18) to display all the items being purchased by that client in this session. See Figure 6-7 for a typical result.

Figure 6-7. The ShowPurchases servlet after the client makes three small acquisitions. The servlet is invoked with the registered name (DisplayPurchases) and uses the sucker.jsp page to present the results.


Listing 6.16. boats/WEB-INF/classes/moreservlets/ShowPurchases.java[*] [*]
package moreservlets; 

import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 

/** A simple servlet that shows a table of purchases. */ 

public class ShowPurchases extends HttpServlet {
  public void doGet(HttpServletRequest request, 
                    HttpServletResponse response) 
      throws ServletException, IOException {
    String itemNum = request.getParameter("itemNum"); 
    ItemTable shipTable = ShipTable.getShipTable(); 
    SimpleItem item = shipTable.getItem(itemNum); 
    HttpSession session = request.getSession(true); 
    ItemList previousItems = 
      (ItemList)session.getAttribute("items"); 
    if (previousItems == null) {
      previousItems = new ItemList(); 
      session.setAttribute("items", previousItems); 
    } 
    previousItems.setNewItem(item); 
    RequestDispatcher dispatcher = 
      getServletContext().getRequestDispatcher("/sucker.jsp"); 
    dispatcher.forward(request, response); 
  } 
} 

[*] Technically, only the.class file needs to go in this directory.

Note that the ItemList class (Listing 6.17) uses an ArrayList, not a Vector. Version 2.3 of the servlet API mandates the use of the Java 2 platform, so this usage does not limit portability.

Listing 6.17. boats/WEB-INF/classes/moreservlets/ItemList.java[*] [*]
package moreservlets; 

import java.util.*; 

/** Very simple pseudo shopping cart. Maintains a list 
 *  of items and can format them in an HTML table. 
 *  Used in the boats Web app example to show that 
 *  each Web app maintains its own set of sessions. 
 */ 

public class ItemList {
  private ArrayList items = new ArrayList(); 
  public synchronized void setNewItem(SimpleItem newItem) {
    if (newItem != null) {
      items.add(newItem); 
    } 
  } 

  public synchronized String getItemTable() {
    if (items.size() == 0) {
      return("<H3>No items...</H3>"); 
    } 
    String tableString = 
      "<TABLE BORDER=1>
" + 
      "  <TR CLASS="COLORED">
" + 
      "      <TH>Item Number
" + 
      "      <TH>Description
" + 
      "      <TH>Cost
"; 
    for(int i=0; i<items.size(); i++) {
      SimpleItem item = (SimpleItem)items.get(i); 
      tableString += 
        "  <TR><TD>" + item.getItemNum() + "
" + 
        "      <TD>" + item.getDescription() + "
" + 
        "      <TD>" + item.getCostString() + "
"; 
    } 
    tableString += "</TABLE>"; 
    return(tableString); 
  } 

  public synchronized String toString() {
    return("[Item List: " + items.size() + " entries.]"); 
     } 
} 

[*] Technically, only the.class file needs to go in this directory.

Listing 6.18. boats/sucker.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE>There's one of these born every minute...</TITLE> 
<LINK REL=STYLESHEET 
      HREF="app-styles.css" 
      TYPE="text/css"> 
</HEAD> 

<BODY> 
<TABLE BORDER=5 ALIGN="CENTER"> 
  <TR><TH CLASS="TITLE">Thanks for Ordering</TABLE> 

<H2>Your Purchases</H2> 
<jsp:useBean id="items" 
             class="moreservlets.ItemList" 
             scope="session" /> 
<jsp:getProperty name="items" property="itemTable" /> 

<%-- Note the lack of "boats" at the front of URI below --%> 
<%@ taglib uri="/WEB-INF/tlds/count-taglib.tld" prefix="boats" %> 
<boats:count /> 
</BODY> 
</HTML> 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.156.107