<jsp:useBean>

The <jsp:useBean> action associates a Java bean with a name in one of the JSP scopes and makes it available as a scripting variable. An attempt is first made to find a bean with the specified name in the specified scope. If it’s not found, a new instance of the specified class is created.

The <jsp:useBean> action supports the attributes described in Table 1-11.

Table 1-11. Attributes for <jsp:useBean>

Name

Java type

Request-time value accepted

Description

beanName

String

yes

Optional. The name of the bean, as expected by the instantiate() method of the Beans class in the java.beans package.

class

String

no

Optional. The fully qualified class name for the bean.

id

String

no

Mandatory. The name to assign to the bean in the specified scope and the name of the scripting variable.

scope

String

no

Optional. The scope for the bean: one of page, request, session, or application. The default is page.

type

String

no

Optional. The fully qualified type name for the bean (i.e., a superclass or an interface implemented by the bean’s class).

Of the optional attributes, at least one of class or type must be specified. If both are specified, class must be assignable to type. The beanName attribute must be combined with the type attribute and is not valid with the class attribute.

The action is processed in these steps:

  1. Attempt to locate an object based on the id and scope attribute values.

  2. Define a scripting language variable with the ...

Get JavaServer Pages Pocket Reference 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.