<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.

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 given id of the specified type or class.

  3. If the object is found, initialize the variable's value with a reference to the located object, cast to the specified type. This completes the processing of the action. If the action element has a nonempty body, it is ignored.

  4. If the object is not found in the specified scope and neither class nor beanName is specified, an InstantiationException is thrown. This completes the processing of the action.

  5. If the object is not found in the specified scope and the class attribute specifies a nonabstract class with a public no-args constructor, a new instance of the class is created and associated with the scripting variable and the specified name in the specified scope. After this, step 7 is performed.

    If the object is not found and the specified class doesn't fulfill the requirements, an InstantiationException is thrown. This completes the processing of the action.

  6. If the object is not found in the specified scope and the beanName attribute is specified, the instantiate() method of the java.beans.Beans class is invoked with the ClassLoader of the JSP implementation class instance and the beanName as arguments. If the method succeeds, the new object reference is associated with the scripting variable and the specified name in the specified scope. After this, step 7 is performed.

  7. If the action element has a nonempty body, the body is processed. The scripting variable is initialized and available within the scope of the body. The text of the body is treated as elsewhere: if there is template text, it is passed through to the response; scriptlets and action tags are evaluated.

    A nonempty body is commonly used to complete initialization of the created instance. In such a case, the body typically contains <jsp:setProperty> actions and scriptlets. This completes the processing of the action.

Example:

<jsp:useBean id="clock" class="java.util.Date" />
beanName String
Request-time value accepted: yes

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

class String
Request-time value accepted: no

Optional. The fully qualified class name for the bean.

id String
Request-time value accepted: no

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

scope String
Request-time value accepted: no

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

type String
Request-time value accepted: no

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