13.1. Basic Bean Use

The jsp:useBean action lets you load a bean to be used in the JSP page. Beans provide a very useful capability because they let you exploit the reusability of Java classes without sacrificing the convenience that JSP adds over servlets alone.

The simplest syntax for specifying that a bean should be used is:

<jsp:useBean id="name" class="package.Class" />

This usually means “instantiate an object of the class specified by Class, and bind it to a variable with the name specified by id.” So, for example, the JSP action

<jsp:useBean id="book1" class="coreservlets.Book" />

can normally be thought of as equivalent to the scriptlet

<% coreservlets.Book book1 = new coreservlets.Book(); %>

Although it is convenient to think ...

Get Core Servlets and JavaServer Pages™ now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.