JSPs in the WebAuction Application

As discussed in Chapter 2, Overview of J2EE Technologies, the WebAuction application includes a suite of JSP pages that handle the user interaction. In this section, we detail one of those JSP pages: browseitems.jsp, and the JavaBean ItemBean that encapsulates the Java code so that it does not appear in the JSP page.

The browseitems.jsp does what its name implies. This JSP page is called when a user wants to look at the current auction items. There are different categories of items up for auction including books, computers, and clothing. When a request comes in to view items of a certain type, the ItemBean queries the appropriate back end resources (in this case, EJBs), and outputs the data.

browseitems.jsp

To build the browseitems.jsp, begin with the basic HTML page. First, define the document type, HTML header, and body style:

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
   <title>WebLogic Server WebAuction: Browse Items</title>
</head>
<body text="#000000" bgcolor="#FFFFFF">

The content type for this HTML document is Version 4.0. Subsequent tags specify the HTML document title and set the colors for the body of the HTML document.

Next, we specify that this page is visible only in the context of a session. We also specify an errorPage for this JSP:

<%@ page session="true" errorPage="error.jsp" %>

The page also uses a JavaBean. The following tag directs WebLogic Server to instantiate an instance of webauction.jsp.ItemBean and makes that available for the JSP page under the identifier "itembean":

<jsp:useBean id="itembean" scope="page"
 class="webauction.jsp.ItemBean" />

Next, we define some text and a table for the links for the different categories:

<H2>Select a category to browse for Items</H2>

<CENTER>
<table width="100%" bgcolor="#0000ff" fgcolor="#FFFFFF">

<TR>

The links contained in this page enable users to browse through the categories of auction items. If a user clicks on one of those links, a parameter is created named cat, which is short for category. We create links that pass this parameter automatically when clicked by adding the parameter and value to the URL. For example, a relative URL of "browseitems.jsp?cat=books" passes the parameter cat with a value of books:

<TD ALIGN="CENTER"><A
href="browseitems.jsp?cat=books"><H4>Books</H4></A></TD>
<TD ALIGN="CENTER"><A href="browseitems.jsp?cat=clothing"><H4>Cloth-
ing</H4></A></TD>
<TD ALIGN="CENTER"><A href="browseitems.jsp?cat=computers"><H4>Comput-
ers</H4></A></TD>
<TD ALIGN="CENTER"><A href="browseitems.jsp?cat=electronics"><H4>Electron-
ics</H4></A></TD>


</TR>

</table>

</CENTER>

When a user chooses to browse a category by clicking on a link, the following scriptlet invokes the JavaBean and deals with it appropriately. First, the category is recognized by looking for it in the request using the getParameter() method on the implicit object request. If the category exists, a method on the JavaBean is called to request all the items in the category. In addition, the current user's name is located in the session object and sent to the JavaBean.

<%
  String category = request.getParameter("cat");

  if (category != null) {

    String userName = (String) session.getAttribute("username");
    itembean.outputItemsInCategory(out, category, userName);
  }
%>

Finally, the JSP page is finished by adding the appropriate closing HTML tags:

</body>
</html>

You should note that very little Java code is encapsulated in this JSP page. This is a good thing: It makes the page more maintainable and presents less of your Java code in a form that Web developers can see and change. This JSP page relies on a JavaBean to do the real work, as discussed in the next section.

ItemBean JavaBean

The ItemBean JavaBean encapsulates the Java code for accessing the back end resources to locate auction items. It can do two things: locate all items in a given category, and also add a new item to the auction.

In the case of browseitem.jsp, the ItemBean is used only to view items up for auction. The capability to enter items is coded in different JSP pages. This is a good example of how JavaBeans enable code to be reused among multiple JSP pages in a Web application.

The ItemBean also relies on EJBs, which are accessed by standard Java calls. EJBs are described in Chapters 8 through 10.

To build ItemBean, first declare the package name and import the necessary classes:

package webauction.jsp;

import java.io.Writer;
import java.sql.Date;
import java.util.Calendar;
import java.util.Collection;
import java.util.Iterator;

Next, define the class and the constructor for this JavaBean:

public final class ItemBean {

  public ItemBean() {
  }

The enterItem method (to be added later) enables the user to create a new auction item:

    public void enterItem(…)
    throws Exception
  {
/* Here, we will add the code for the method to enter an item
into the auction.
*/
  }

The outputItemsInCategory method is called by the browseitems.jsp. Three parameters are passed to this method including the output writer, the category string, and the current user's name:

  public void outputItemsInCategory(Writer out, String category,
    String userName)
  {
   /* This section eventually accesses the WebAuction
      EJB and locates a Collection of all the items in the pro-
vided category.
    */

    Collection items = itemHome.findItemsInCategory(category);

   /* If the Collection is empty, then print an appropriate mes-
sage
    */
    if (items.isEmpty()) {
      out.write("<H2>There is nothing in the "+category+
                " category currently available for bidding.");
    } else {

      out.write("<table border=2 cellspacing=2 cellpadding=2");

      out.write("<TR><TD>Category</TD> "+
        "<TD>Description</TD><TD>Offerred by</TD>"+
        "<TD>Top Bidder</TD>"+
        "<TD>Top Bid Amount</TD><TD>Auction Ends</TD></TR>");
      Iterator it = items.iterator();

      while (it.hasNext()) {

        Item item = (Item) it.next();

        String topBidString = null;
        String topBidUser = "None";

 /* In future chapters, we'll add code to query the EJB for
    the data on the item up for bid here.
*/
.
.
.

        /* Next, print out data about the item. */
        out.write("<TR>" +
          "<TD>" + item.getCategory() + "</TD>" +
          "<TD>"+ item.getDescription() + "</TD>" +
   "<TD>"+ topBidUser + "</TD>" +
          "<TD>"+topBidString+"</TD>"+
          "<TD>"+item.getAuctionEnd()+"</TD>"+
          "<TD BGCOLOR="0x0000FF"><A href="bid.jsp?id="+
item.getId() +
          "">Bid on this item</A></TD>");

}
        out.write("</TR>");
      }
      out.write("</table>");
    }
  }

}

In subsequent chapters, additional functionality is added to ItemBean, such as the capability to interact with back end resources. For now, we have a shell that we can fill in with data access code as we continue to develop the WebAuction application.

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

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