The <jsp:useBean> tag locates or instantiates a JavaBeans component. The <jsp:useBean> tag 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:
For more information, see the JavaServer Pages (JSP) v1.2 Syntax Reference on the Sun Microsystems® web site.
<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 tags </jsp:useBean> }
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 name must conform to the naming 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"
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 as follows:
page: You can use the bean within the JSP page with the <jsp:useBean> tag 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"
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.
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 type must be a superclass of class or an interface implemented by class. If you use type without class or beanName, no bean is instantiated. The package and class name are case sensitive.
class="package.class" type="package.class"
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.
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 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.
<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>