jsp:useBean

The jsp:useBean action defines an instance of a Java bean in a JSP page. Once defined, you can then reference the bean in the JSP file.

In the following example, you define a bean named myBean of type com.myco.myapp.MyBean:

<jsp:useBean id="myBean" class="com.myco.myapp.MyBean" />

The basic syntax of jsp:useBean is shown below:

<jsp:useBean id="name" scope="page|request|session|application" 
    typeSpec />

where typespec is any one of the following:

    class="className" |
    class="className" type="typeName" |
    beanName="beanName" type=" typeName" |
    type="typeName"

You must specify either type or class; it is not valid to specify both class and beanName. If you specify both type and class, then class must be assignable to type.

The attribute beanName is the name of a bean, in the form "a.b.c", which may be either a class or the name of a resource in the form "a/b/c.ser".

Additionally, you can specify a body to jsp:useBean in the form:

<jsp:useBean id="name" scope="page|request|session|application"
    typeSpec >
    body
</jsp:useBean>

The body is invoked if the bean is created. Typically, the body will contain either scriptlets or jsp:setProperty tags used to modify the newly created bean; but the content of the body is not restricted.

The <jsp:useBean> tag has the following attributes:

id

Specifies the name used to identify the bean in the specified scope, and also the scripting variable name for the bean. The specified name is case sensitive and must conform to the scripting language's variable-naming conventions.

scope

Optionally defines the scope within which the bean is available. The default value is page.

class

Sets the fully qualified name of the class that defines the implementation of the bean. The class name is case sensitive.

If you omit the class and beanName attributes the object must be present in the specified scope.

beanName

Specifies the name of a bean, as recognized by the instantiate method of the java.beans.Beans class.

This attribute can accept a request-time attribute expression as a value.

type

If specified, type defines the type of the scripting variable. The bean must be an instance to the specified type.

This attribute allows the type of the scripting variable to be distinct from, but related to, that of the specified implementation class. The type is required to be either the class itself, a superclass of the class, or an interface implemented by the specified class.

If you omit this parameter, the type is the same as the value of the class attribute.