Java Server Pages: Beans

JavaBeans are reusable Java components that allow us to separate business logic from presentation logic. Technically, a JavaBean class is a Java class that meets the following requirements:

  • It has a public, no argument constructor.
  • It implements the io.Serializable Or java.io.Externalizable interface.
  • Its properties are accessible using methods that are written following a standard naming convention.

With this simple definition, properly designed beans can be used virtually in all Java environments such as JSP, servlet, applet, or even in Java applications. Note that most of the existing classes are either already bean classes or they can be converted to bean classes very easily.

The methods of a bean must follow a naming convention. If the name of a bean property is xxx, the associate reader and writer method must have the name getXxx() and setXxx(), respectively. The following is a sample class declaration for JavaBean Factorial:

package bean;

public class Factorial implements java.io.Serializable {

int n;

public int getValue() {

int prod = 1;

for(int i = 2; i <= n; i++) prod *= i;

return prod;

}

public void setValue(int v) {

n = v;

}

}

Since no constructor is defined explicitly in this class, a zero argument constructor is inserted in the Factorial class. The Factorial bean class has one property, value. So, the name of the reader method is getValue(). Similarly, the name of the writer method is setValue(). The name of the member variable need not be value; it is the property that we want to manipulate on a Factorial bean.

Save this code in the file Factorial.java and store it in the application’s /WEB-INF/classes/ bean directory. Compile the class exactly like other Java classes. Use the following command in the /WEB-INF/classes/bean directory.

javac Factorial.java

This generates a class file, Factorial.class. If everything goes fine, restart the tomcat web server. Tomcat loads all the class files under / classes directory and adds its subdirectories to the CLASSPATH of the Java Runtime Environment (JRE). Those class files can now be used exactly like other Java classes.

There are three action elements that are used to work with beans.

1. useBean

A JSP action element <jsp:useBean> instantiates a JavaBean object into the JSP page. The syntax is

<jsp:useBean id=”object_name” class=”class_name” scope=”page | request | session |application”/>

Here, the id attribute refers to the name of the object to be created and the attribute class specifies the name of the JavaBean class from which the object will be instantiated. The attribute scope specifies the area of visibility of the loaded bean. The effect of the <jsp:useBean> element is equivalent to instantiating an object as follows:

<% class_name object_name = new class_name(); %>

For example, to instantiate a Factorial bean in a JSP page, the following action is used:

<jsp:useBean id=”fact” scope=”page” class=”bean.Factorial” />

This is equivalent to the following scriptlet:

<% bean.Factorial fact = new bean.Factorial(); %>

Once a bean object is loaded into a JSP page, we can use two other action elements to manipulate it.

2. setProperty

The <jsp:setProperty> action tag assigns a new value to the specified property of the specified bean object. It takes the following form:

<jsp:setProperty name=”obj_name” property=”prop_name” value=”prop_value”/>

The object name, property name, and its value are specified by the name, property, and value attributes, respectively. This is equivalent to calling the setProp_name() method on the specified object obj_name as follows:

<% obj_name.setProp_name(prop_value); %>

To set a property of our bean object fact, we use the following:

<jsp:setProperty name=”fact” property=”value” value=”5” />

The equivalent scriptlet is as follows:

<% fact.setValue(5); %>

3. getProperty

The <j sp: getProperty> action element retrieves the value of the specified property of the specified bean object. The value is converted to a string. It takes the following form:

<jsp:getProperty name=”obj_name” property=”prop_name”/>

The object name and its property name are specified by the name and property attributes, respectively. This is equivalent to calling the getProp_name() method on the specified object obj_name as follows:

<%= obj_name.getProp_name() %>

To get the value of the property value of our fact bean object, we use the following:

<jsp:getProperty name=”fact” property=”value” />

The equivalent scriptlet is as follows:

<%= fact.getValue() %>

4. Complete Example

The following is a complete JSP page:

<table border=”1”>

<caption>Factorial table</caption>

<tr><th width=”50”>n</th><th width=”100’’>n!</th></tr>

<jsp:useBean id=”fact” scope=npagen class=”bean.Factorial” />

<%

for(int i = 2; i < 6; i++)                 {

%>

<jsp:setProperty name=”fact” property=”value” value=”<%=i%>” /> <tr><td><%=i%></td><td><jsp:getProperty name=”fact” property=”value” />

</td></tr>

<%

}

%>

</table>

It generates the output shown in Figure 21.8:

Figure 21.8: Using Beans in JSP

5. Other Usage

Once a bean object is loaded into the page, it can be used exactly like other objects in scripting elements in the same JSP page. Consider the following code:

<jsp:useBean id=”fact” scope=”page” class=”bean.Factorial” />

This is equivalent to the following instantiation:

<% bean.Factorial fact = new bean.Factorial(); %>

Now, the bean object can be accessed using its name, as follows:

<%fact.setValue(6);%>

<%=fact.getValue()%>

This displays the following output:

720

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *