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
" ]|
beanName="{
package.class
|
<%=
expression
%>}" type="
package.class
" | type="package.class
" } {/> | >
other elements
</jsp:useBean> }
<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> }
<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.
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.
id="
beanInstanceName
"
<jsp:useBean>
element, the value of id
must match the value of id
used in the original <jsp:useBean>
element.
scope="
page|request|session|application"
id
is available. The default value is page
. The meanings of the different scopes are shown below:
page
You can use the bean within the JSP page with the <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 resource.
request
You 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 the request
object to access the bean, for example, request.getAttribute(
beanInstanceName
)
.
session
You 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 with session="true"
.
application
You 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.
class="
package.class
"
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.
type="
package.class
"
type
must be a superclass of class
or an interface implemented by class
.
type
without class
or beanName
, no bean is instantiated. The package and class name are case sensitive.
class="
package.class
" type="
package.class
"
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
.
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.
beanName="{
package.class
| <%=
expression
%>}" type="
package.class
"
beanName
, the bean is instantiated by the java.beans.Beans.instantiate
method. The Beans.instantiate
method checks whether the package and class you specify represents a class or a serialized template. If they represent a serialized template, Beans.instantiate
reads the serialized form (which has a name like package.class.ser
) using a class loader.
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.