| Actions in JSP |
| |
| More jsp:useBean Details |
| |
| The simplest way to use a bean is to use |
| <jsp:useBean id="name" class="package.class" /> |
| |
| to load the bean, then use jsp:setProperty and jsp:getProperty to modify and retrieve bean properties. |
| However, there are two other options. |
| |
| First, we can use the container format, namely |
<jsp:useBean ...>
Body
</jsp:useBean> |
| to indicate that the Body portion should be executed only when the bean is first instantiated, not when an existing bean is found and used. |
| |
| Beans can be shared, so not all jsp:useBean statements result in a new bean being instantiated. |
| |
| Second, in addition to id and class, there are three other attributes that we can use: |
scope,
type,
and beanName. |
| |
| These attributes are summarized in the following table. |
| |
| Atribute |
Usage |
| id |
Gives a name to the variable that will reference the bean. A previous bean object is used instead of instantiating a new one if one can be found with the same id and scope. |
| class |
Designates the full package name of the bean. |
| scope |
Indicates the context in which the bean should be made available. There are four possible values: page, request, session, and application. The default, page, indicates that the bean is only available on the current page (stored in the PageContext of the current page). A value of request indicates that the bean is only available for the current client request (stored in the ServletRequest object). A value of session indicates that the object is available to all pages during the life of the current HttpSession. Finally, a value of application indicates that it is available to all pages that share the same ServletContext. The reason that the scope matters is that a jsp:useBean entry will only result in a new object being instantiated if there is no previous object with the same id and scope. Otherwise the previously existing object is used, and any jsp:setParameter elements or other entries between the jsp:useBean start and end tags will be ignored. |
| type |
Specifies the type of the variable that will refer to the object. This must match the classname or be a superclass or an interface that the class implements. Remember that the name of the variable is designated via the id attribute. |
| beanName |
Gives the name of the bean, as you would supply it to the instantiate method of Beans. It is permissible to supply a type and a beanName, and omit the class attribute. |
|
| |
| |
|
| |
| |