Using JavaBeans with JSP
- Using <jsp:useBean>
- Using <jsp:getProperty>
- Using <jsp:setProperty>
JavaBeans is the component architecture for Java. JavaBeans is often a confusing term because it is often used to refer to the entire architecture as well as to the individual beans. Technically, JavaBeans refers to the architecture, and the components are referred to simply as beans. JavaBeans components are reusable. In fact, beans are very portable and can be used in different environments.
NOTE
A JavaBeans component is not the same as an Enterprise JavaBeans component.
Graphical user interface (GUI) application programmers often use beans in order to reuse buttons and other objects. In Web programming, beans can be used to perform specific functions, such as outputting dates, interacting with a database, and even performing important functions specific to a company.
NOTE
If you are interested in developing your own beans, you can do so by using the JavaBeans API. For more information and to download the Bean Developer Kit (BDK), go to http://java.sun.com/products/javabeans/software.
TIP
The Sun Web site has a directory of bean components available for purchase at http://industry.java.sun.com/solutions/browse/0,2346,beans,00.html.
There are basically two types of beans that you will be using in applications: entity beans and utility beans. Value beans are used to store data, and can be assigned to different scopes, including session and application, which means you can store things like shopping cart data in beans that need to exist in those scopes. Utility beans are typically used to process data, including formatting, sending e-mail messages, validating form data, and handling other processing directives.
Using <jsp:useBean>
To use a JavaBeans component, the first thing you need to do is to enable the use of a bean within your current template, through a process called instantiation. You use the <jsp:useBean> action to instantiate beans. Table 1 shows the attributes of this action.
Table 1 Attributes of the <jsp:useBean> Action
Attribute |
Use |
id |
This attribute specifies the name of the bean and how you will refer to it in the page. |
scope |
This attribute specifies the scope in which you want to store the bean instance. It can be set to page (the default), session, request, or application. |
class |
This attribute specifies the Java class that the bean is drawn from. If you have specified beanName, you do not have to specify class. |
beanName |
This attribute specifies the name of a bean that is stored on the server. You refer to it as you would a class (for example, com.projectalpha.PowerBean). If you have specified class, you do not need to specify beanName. |
type |
This attribute specifies the type of scripting variable returned by the bean. The type must relate to the class of the bean. |
The following is a simple example of using <jsp:useBean> to instantiate java.util.Date as an entity bean:
<jsp:useBean id="today" class="java.util.Date" />
After a bean is instantiated, you can use it in two ways. First, two actions, <jsp:getProperty> and <jsp:setProperty>, allow you to set values and retrieve values in a bean. Second, you can directly access the methods within a bean by using Java code in scriptlets.