Contents | Prev


<jsp:useBean>

Locates or instantiates a bean with a specific name and scope.

JSP Syntax

<jsp:useBean id="beanInstanceName"	
   scope="page|request|session|application"	
{	
   class="package.class" [ type="package.class" ]|	
   beanName="{package.class | <%= expression %>}" 	
      type="package.class" | 	
   type="package.class"	
}	
{ /> | > other elements </jsp:useBean> }

XML Syntax

<jsp:useBean id="beanInstanceName"	
   scope="page|request|session|application"	
{	
   class="package.class"  [ type="package.class" ]  |	
   beanName="{package.class | %= expression %}"	
      type="package.class" |	
   type="package.class"	
}	
{ /> | > other elements </jsp:useBean>  }

Examples

<jsp:useBean id="cart" scope="session" class="session.Carts" />	
<jsp:setProperty name="cart" property="*" />
<jsp:useBean id="checking" scope="session" class="bank.Checking" >	
   <jsp:setProperty name="checking" property="balance" value="0.0" />	
</jsp:useBean>

Description

The <jsp:useBean> element locates or instantiates a JavaBeans component. <jsp:useBean> first attempts to locate an instance of the bean. If the bean does not exist, <jsp:useBean> instantiates it from a class or serialized template.

To locate or instantiate the bean, <jsp:useBean> takes the following steps, in this order:

  1. Attempts to locate a bean with the scope and name you specify.
  2. Defines an object reference variable with the name you specify.
  3. If it finds the bean, stores a reference to it in the variable. If you specified type, gives the bean that type.
  4. If it does not find the bean, instantiates it from the class you specify, storing a reference to it in the new variable. If the class name represents a serialized template, the bean is instantiated by java.beans.Beans.instantiate.
  5. If <jsp:useBean> has instantiated (rather than located) the bean, and if it has body tags or elements (between <jsp:useBean> and </jsp:useBean>), executes the body tags.

The body of a <jsp:useBean> element often contains a <jsp:setProperty> element that sets property values in the bean. As described in Step 5, the body tags are only processed if <jsp:useBean> instantiates the bean. If the bean already exists and <jsp:useBean> locates it, the body tags have no effect.

You can use a <jsp:useBean> element to locate or instantiate a JavaBeans component , but not an enterprise bean. To create enterprise beans, you can write a <jsp:useBean> element that calls a bean that in turn calls the enterprise bean, or you can write a custom tag that calls an enterprise bean directly.

Attributes and Usage

See Also



Contents | Prev

Copyright © 2002, Sun Microsystems, Inc. All rights reserved.