Locates or instantiates a Bean with a specific name and scope.
<jsp:useBean
id="beanInstanceName"
scope="page | request | session | application"
{
class="package.class" |
type="package.class" |
class="package.class" type="package.class" |
beanName="{package.class | <%= expression %>}" type="package.class"
}
{
/> |
> other elements </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" />
</jsp:useBean>
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:
type, gives the Bean that type.
java.beans.Beans.instantiate.
<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.
In this release, you can use a <jsp:useBean> element to locate or instantiate a Bean, 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.
A variable that identifies the Bean in the scope you specify. You can use the variable name in expressions or scriptlets in the JSP file.
The name is case sensitive and must conform to the naming conventions of the scripting language used in the JSP page. If you use the Java programming language, the conventions in the Java Language Specification. If the Bean has already been created by another <jsp:useBean> element, the value of id must match the value of id used in the original <jsp:useBean> element.
The scope in which the Bean exists and the variable named in id is available.
The default value is page.
The meanings of the different scopes are shown below:
<jsp:useBean> element or any of the page's static include files, until the page sends a response back to the client or forwards a request to another file.
request object to access the Bean, for example, request.getAttribute(beanInstanceName).
<%@ page %> directive with session=true.
Instantiates a Bean from a class, using the new keyword and the class constructor. The class must not be abstract and must have a public, no-argument constructor. The package and class name are case sensitive.
If the Bean already exists in the scope, gives the Bean a data type other than the class from which it was instantiated. If you use type without class or beanName, no Bean is instantiated. The package and class name are case sensitive.
Instantiates a Bean from the class named in class and assigns the Bean the data type you specify in type. The value of type can be the same as class, a superclass of class, or an interface implemented by class.
The class you specify in class must not be abstract and must have a public, no-argument constructor. The package and class names you use with both class and type are case sensitive.
Instantiates a Bean from either a class or a serialized template, using the java.beans.Beans.instantiate method, and gives the Bean the type specified in type. The Beans.instantiate method checks whether a name represents a class or a serialized template. If the Bean is serialized, Beans.instantiate reads the serialized form (with a name like package.class.ser) using a class loader. For more information, see the JavaBeans API Specification.
The value of beanName is either a package and class name or an
Expression that evaluates to a package and class name, and is passed to Beans.instantiate. The value of type can be the same as beanName, a superclass of beanName, or an interface implemented by beanName.
The package and class names you use with both beanName and type are case sensitive.
<jsp:setProperty>
<jsp:getProperty>
java.beans.Beans