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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.