<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 '}' |<%=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 '}' |
%= 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:
- Attempts to locate a bean with the scope and name you specify.
- Defines an object reference variable with the name you specify.
- If it finds the bean, stores a reference to it in the variable. If you specified
type, gives the bean that type. - 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. - If
jsp:useBeanhas 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
id="beanInstanceName"- A variable that identifies the bean in the scope you specify. You can use the variable name in expressions or scriptlets in the JSP page.
- 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:useBeanelement, the value ofidmust match the value ofidused in the originaljsp:useBeanelement. - 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
- A variable that identifies the bean in the scope you specify. You can use the variable name in expressions or scriptlets in the JSP page.
scope="page|request|session|application"- The scope in which the bean exists and the variable named in
idis available. The default value ispage. The meanings of the different scopes are shown below:pageYou can use the bean within the JSP page with thejsp:useBeanelement 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 resource.requestYou can use the bean from any JSP page processing the same request, until a JSP page sends a response to the client or forwards the request to another resource. You can use therequestobject to access the bean, for example,request.getAttribute(beanInstanceName).sessionYou can use the bean from any JSP page in the same session as the JSP page that created the bean. The bean exists across the entire session, and any page that participates in the session can use it. The page in which you create the bean must have a page directive withsession="true".applicationYou can use the bean from any JSP page in the same application as the JSP page that created the bean. The bean exists across an entire JSP application, and any page in the application can use the bean.
- The scope in which the bean exists and the variable named in
class="package.class"- Instantiates a bean from a class, using the
newkeyword 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.
- Instantiates a bean from a class, using the
type="package.class"- If the bean already exists in the scope, gives the bean a data type other than the class from which it was instantiated. The value of
typemust be a superclass ofclassor an interface implemented byclass.- If you use
typewithoutclassorbeanName, no bean is instantiated. The package and class name are case sensitive. - If you use
- If the bean already exists in the scope, gives the bean a data type other than the class from which it was instantiated. The value of
class="package.class" type="package.class"- Instantiates a bean from the class named in
classand assigns the bean the data type you specify intype. The value oftypecan be the same asclass, a superclass ofclass, or an interface implemented byclass.- The class you specify in
classmust not be abstract and must have a public, no-argument constructor. The package and class names you use with bothclassandtypeare case sensitive. - The class you specify in
- Instantiates a bean from the class named in
beanName="{package.class| <%=expression%>}" type="package.class"- Instantiates a bean from a class, a serialized template, or an expression that evaluates to a class or serialized template. When you use
beanName, the bean is instantiated by thejava.beans.Beans.instantiatemethod. TheBeans.instantiatemethod checks whether the package and class you specify represents a class or a serialized template. If they represent a serialized template,Beans.instantiatereads the serialized form (which has a name likepackage.class.ser) using a class loader.- The value of
typecan be the same asbeanName, a superclass ofbeanName, or an interface implemented bybeanName. The package and class names you use with bothbeanNameandtypeare case sensitive. - The value of
- Instantiates a bean from a class, a serialized template, or an expression that evaluates to a class or serialized template. When you use
