background image

JavaBeans Components

<< Defining Functions | Creating and Using JavaBeans Components >>
<< Defining Functions | Creating and Using JavaBeans Components >>

JavaBeans Components

JavaBeans Components
JavaBeans components are Java classes that can be easily reused and composed together into
applications. Any Java class that follows certain design conventions is a JavaBeans component.
JavaServer Pages technology directly supports using JavaBeans components with standard JSP
language elements. You can easily create and initialize beans and get and set the values of their
properties.
JavaBeans Component Design Conventions
JavaBeans component design conventions govern the properties of the class and govern the
public methods that give access to the properties.
A JavaBeans component property can be:
Read/write, read-only, or write-only
Simple, which means it contains a single value, or indexed, which means it represents an
array of values
A property does not have to be implemented by an instance variable. It must simply be
accessible using public methods that conform to the following conventions:
For each readable property, the bean must have a method of the form:
PropertyClass getProperty() { ... }
For each writable property, the bean must have a method of the form:
setProperty(PropertyClass pc) { ... }
In addition to the property methods, a JavaBeans component must define a constructor that
takes no parameters.
The Duke's Bookstore application JSP pages bookstore.jsp, bookdetails.jsp, catalog.jsp,
and showcart.jsp, all located at
tut-install/javaeetutorial5/examples/web/bookstore2/web, use the
tut-install/javaeetutorial5/examples/web/bookstore2/src/java/com/sun/bookstore2/database/BookDB.java
JavaBeans component.
BookDB
provides a JavaBeans component front end to the access object BookDBAO. The JSP pages
showcart.jsp
and cashier.jsp access the bean
tut-install/javaeetutorial5/examples/web/bookstore/src/com/sun/bookstore/cart/ShoppingCart.java,
which represents a user's shopping cart.
The BookDB bean has two writable properties, bookId and database, and three readable
properties: bookDetails, numberOfBooks, and books. These latter properties do not correspond
to any instance variables but rather are a function of the bookId and database properties.
JavaBeans Components
Chapter 5 · JavaServer Pages Technology
167