|
Home TOC |
|
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 Bookstorepagesbookdetails.jsp,catalog.jsp, andshowcart.jspreplace the scriptlet with the much more concise JSPuseBeanelement:<jsp:useBean id="cart" class="cart.ShoppingCart" scope="session"/>
|
Home TOC |
|