The JavaTM Web Services Tutorial
Home
TOC
Index
PREV TOP NEXT
Divider

Why Use a JavaBeans Component?

A JSP page can create and use any type of Java programming language object within a declaration or scriptlet. The following scriptlet creates the bookstore shopping cart and stores it as a session attribute:

<% 
  ShoppingCart cart = (ShoppingCart)session.
    getAttribute("cart");
  // If the user has no cart, create a new one
  if (cart == null) {
    cart = new ShoppingCart();
    session.setAttribute("cart", cart);
  }
%>
 

If the shopping cart object conforms to JavaBeans conventions, JSP pages can use JSP elements to create and access the object. For example, the Duke's Bookstore pages bookdetails.jsp, catalog.jsp, and showcart.jsp replace the scriptlet with the much more concise JSP useBean element:

<jsp:useBean id="cart" class="cart.ShoppingCart"
  scope="session"/>
 
Divider
Home
TOC
Index
PREV TOP NEXT
Divider

This tutorial contains information on the 1.0 version of the Java Web Services Developer Pack.

All of the material in The Java Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.