In this tutorial you will learn about the useBean action in JSP.
JSP useBean Action
In this tutorial you will learn about the useBean action in JSP.
useBean in JSP is used to find out or to instantiate the component of JavaBeans with a specified name and scope. At first the useBean tries to find out an instance of the Bean, if an instance of Bean is not found / existed then useBean creates an instance of it from a class.
To find out or to instantiate the Bean useBean does the following steps in the order as given below :
Step 1. At first it tries to find out a Bean with the specified name and scope.
Step 2. Determines an object reference variable with the specified name.
Step 3. In case of the Bean found / existed, its reference is to be stored in a variable and defines the Bean type if type is specified by you.
Step 4. In case of the Bean is not find / doesn't exist, it creates an instance from the specified class and its reference is to be stored in a new variable.
Step 5. If the Bean is instantiated by the useBean and if between the useBean tags ( i.e. between <jsp:useBean> </jsp:useBean> ) there is a body tags or elements then executes these tags or elements.
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> }
Description of Attributes are as follows :
- id = "beanInstanceName" : In the specified scope a Bean is identified by this variable. A variable name that is to be used here is a case sensitive.
- scope= "page/request/session/application" : It specifies that in which scope/range the Bean existed. The different scopes which are given here has their own meaning :
- page : When you will use the value 'page' as a scope attribute of <jsp:useBean> then the range of using a Bean will be limit to within a page it means a Bean can be used within the JSP page i.e. it will be used only until a response is sent back to the client or transfer/forward a control to another file.
- request : When you will use the value 'request' as a scope attribute of <jsp:useBean> then the Bean can be used from any of the JSP page but the page should be participating / processing in the same request only till then a JSP page doesn't send a response to the client or transfer / forwards a request to another file.
- session : When you will use the value 'session' as a scope attribute of <jsp:useBean> then the Bean can be used from any of the JSP page in the same session. To use it a page directive must be used with the session = "true" in the page where a Bean has created.
- application : When you will use the value 'application' as a scope attribute of <jsp:useBean> then the Bean can be used from any of the JSP page in the same application.
- class= "package.class" : It specifies the class name from the Bean is to instantiated. Here name of package and class is case sensitive. A Bean is instantiated using the new keyword and public, no-argument constructor. It should be careful while creating a class which will be specifies in 'class' must not be an abstract class and contains public constructor with no-argument.
- type= "package.class" : It is used to give a data type to the Bean except its instantiation class when a defined bean is already existed in the specified scope.
- class="package.class" type="package.class" : It specifies the class named in class from the Bean is to be instantiated and give a specified data type to the Bean specified in type. It should be careful while creating a class which will be specifies in 'class' must not be an abstract class and contains public constructor with no-argument.
- beanName="{package.class | <%= expression %>}" type="package.class" : Using this attribute a Bean is instantiated from either a class or serialized template by the method java.beans.Beans.instantiate method and provide a Bean specific type specified in 'type'.
Note : The default value of scope attribute is 'page'.
[ 0 ] Comments