[Top] [Prev] [Next] [Bottom]


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

JSP Syntax

	{	class="package.class" | 
		type="package.class" | 
		class="package.class" type="package.class" |
		beanName="{ package.class | <%= expression %> }" type="package.class"  
	{ 	/> |  
		> other tags </jsp:useBean>  


<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" />


The <jsp:useBean> tag attempts to locates a Bean, or if the Bean does not exist, 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 it has instantiated (rather than located) the Bean, and if it has body tags (between <jsp:useBean> and </jsp:useBean>), executes the body tags.

The body of a <jsp:useBean> tag often contains a <jsp:setProperty> tag that defines property values in the object. 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.

In JSP 1.0, <jsp:useBean> works with JavaBeans components, but not with enterprise beans. If you want to use enterprise beans, you can write a JSP file that constructs a JavaBean component, and have the JavaBean component call the enterprise bean.

Attributes and Usage

See Also

[Top] [Prev] [Next] [Bottom]

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