<Jsp: useBean> Create a Bean instance and specify its name and scope.
JSP syntax
<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>
}
Example
<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
<Jsp: useBean> is used to locate or sample A JavaBeans component. <Jsp: useBean> first tries to locate a Bean instance. If this Bean does not exist, <jsp: useBean> will take an example from a class or template.
To locate or sample a Bean, <jsp: useBean> performs the following steps:
Attempts to locate a Bean by specifying the name and range.
The variables referenced in this Bean object are named by the name you specified.
If this Bean is found, the reference will be stored in this variable. If you also specify the type, the Bean is also set to the corresponding type.
If this Bean is not found, it will be used from the class you specified and stored in a new variable. If the class name represents a template, the Bean is used by java. beans. Beans. instantiate.
If <jsp: useBean> has been used as an example (not to locate) Bean, and <jsp: useBean> and </jsp: useBean> have elements, the code will be executed.
The main body of the <jsp: useBean> element usually contains the <jsp: setProperty> element, which is used to set the Bean property value. As described in step 5 above? Lt; jsp: useBean> is executed only when the <jsp: useBean> sample Bean exists. If the Bean already exists, <jsp: useBean> can locate it, the content in the subject will not work.
Attributes and usage
Id = "beanInstanceName"
Confirm the Bean variable in the scope you define. You can use this variable name in subsequent programs to distinguish different beans.
This variable name is case sensitive and must comply with the requirements of your scripting language.
In Programming Language, this rule is in Java Language
The specification has been stated. If this Bean has been created in another <jsp: useBean>, the id value must be consistent with the original id value.
Scope = "page | request | session | application"
The valid range of Bean and id variable name. The default value is page, which is described as follows:
Page-you can use the Bean in the jsp file that contains the <JSP: useBean> element and all static include files in the file, after the page is executed, send a response to the client or go to another file.
Request-you can use beans in any Jsp file that executes the same request until the page execution is complete and the response is sent back to the client or transferred to another file. You can use the Request object to access beans, such as request. getAttribute (beanInstanceName)
Session-from Bean creation, you can use Bean in any Jsp file that uses the same session. this Bean exists throughout the Session lifecycle. Any Jsp file that shares this Session can use the same Bean. note that <%
@ Page %> the command must specify session = true.
Application-from Bean creation, you can use Bean in any Jsp file that uses the same application. this Bean exists throughout the application lifecycle. Any Jsp file that shares this application can use the same Bean.
Class = "package. class"
Use the new Keyword and the class constructor to sample a bean from a class. this class cannot be abstract and must have a common constructor without parameters. the package name is case sensitive.
Type = "package. class"
If the Bean already exists in the specified range, write this Bean to a new database type.
. If you do not use the class or beanName to specify the type, Bean will not be named by the example. package and class names, case sensitive.
BeanName = "{package. class | <% = expression %>}" type = "package. class"
Use the java. beans. Beans. instantiate method to sample a Bean from a class or continuous template, and specify the Bean type.
BeanName can be package or class, and its value will be passed to Beans. instantiate. tupe with the same value as Bean.
The package and class names are case sensitive.