background image

Creating and Using JavaBeans Components

<< JavaBeans Components | Setting JavaBeans Component Properties >>
<< JavaBeans Components | Setting JavaBeans Component Properties >>

Creating and Using JavaBeans Components

package database;
public class BookDB {
private String bookId =
"0";
private BookDBAO database = null;
public BookDB () {
}
public void setBookId(String bookId) {
this.bookId = bookId;
}
public void setDatabase(BookDBAO database) {
this.database = database;
}
public Book getBook() throws
BookNotFoundException {
return (Book)database.getBook(bookId);
}
public List getBooks() throws BooksNotFoundException {
return database.getBooks();
}
public void buyBooks(ShoppingCart cart)
throws OrderException {
database.buyBooks(cart);
}
public int getNumberOfBooks() throws BooksNotFoundException {
return database.getNumberOfBooks();
}
}
Creating and Using a JavaBeans Component
To declare that your JSP page will use a JavaBeans component, you use a jsp:useBean element.
There are two forms:
<jsp:useBean id=
"beanName"
class=
"fully-qualified-classname" scope="scope"/>
and
<jsp:useBean id=
"beanName"
class=
"fully-qualified-classname" scope="scope">
<jsp:setProperty .../>
</jsp:useBean>
The second form is used when you want to include jsp:setProperty statements, described in
the next section, for initializing bean properties.
JavaBeans Components
The Java EE 5 Tutorial · September 2007
168