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 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" /></jsp:useBean>
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:
type
, gives the Bean that type. java.beans.Beans.instantiate
. <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.
id="
beanInstanceName"
Names a variable that identifies the Bean in the scope you specify. You can use the variable name in expressions or scriptlets in the same JSP file.
The name is case sensitive and must conform to the naming conventions of the page scripting language (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>
tag, the value of id
must match the value of id
used in the original <jsp:useBean>
tag.
scope="
page|request|session|application"
Defines a scope in which the Bean exists and the variable named in id
is available. The default value is page
.
class="
package.class"
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.
class="
package.class" type="
package.class"
Instantiates a Bean from a class, using the new
keyword and the class constructor, and gives the Bean the type you specify in type
. 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.
The value of type
can be the same as class
, a superclass of class
, or an interface implemented by class
.
beanName="{
package.class | <%=
expression %> }" type="
package.class"
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
. Beans.instantiate
checks whether a name represents a class or a serialized template. If the Bean is a serialized template, 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
is a package and class name. The value of type
can be the same as beanName
, a superclass of beanName
, or an interface implemented by beanName
.
With both beanName
and type
, the package and class names are case sensitive.
type="
package.class"
If the Bean already exists in the specified scope, gives the Bean the type you specify. If you use type
without class
or beanName
, no Bean is instantiated. The package and class name are case sensitive.
index.jsp<%@ page language="java" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>Test the UserBean Tag</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"></head><body><form action="another.jsp"><jsp:useBean id="plainBean" class="com.PlainBean" scope="session"/><jsp:setProperty name="plainBean" property="name" value="John1"/><font color="blue"><jsp:getProperty name="plainBean" property="name"/></font><jsp:useBean id="anotherBean" class="com.AnotherBean" scope="session"><jsp:setProperty name="anotherBean" property="name" value="John2"/><font color="blue"><jsp:getProperty name="anotherBean" property="name"/></font></jsp:useBean><input type="submit" ></form></body></html>another.jsp:<%@ page language="java" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>Test the UserBean Tag</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"></head><body><jsp:useBean id="plainBean" class="com.PlainBean" scope="session"/><jsp:setProperty name="plainBean" property="name" value="Tom"/><font color="blue"><jsp:getProperty name="plainBean" property="name"/></font><jsp:useBean id="anotherBean" class="com.AnotherBean" scope="session"><jsp:setProperty name="anotherBean" property="name" value="Tom"/><font color="blue"><jsp:getProperty name="anotherBean" property="name"/></font></jsp:useBean></body></html>