6. JavaServer Pages and JavaBeans

JSP Actions for JavaBeans

To use a JavaBean in our JSP page we make use of three JSP actions that we saw earlier. They are,

The first of these is used to specify which JavaBean we want to use in our page. The next two actions, as their names suggest, are used to set and get values of properties of the JavaBean.

The useBean Action

Syntax

<jsp:useBean id="beanID" scope="beanScope"
                             class="package.beanName" />

Where BeanID is the variable you use in your code to reference the Bean, beanScope is the scope the Bean has. It can take one of the following values,

Value Description
page It lasts until the page completes and there is no storage of state.
request The JavaBean instance lasts for the client request and so will remain if the request is forwarded.
session The JavaBean lasts as long as the client session.
application The JavaBean is instantiated with the application and remains in use until the application ends.

package and beanName refer to the package and name of the JavaBean you wish to use. Note, some applications servers require that you also use the page import directive to reference the package or class that contains the JavaBean

The setProperty and getProperty Actions

These are used to set and read property values.

Syntax

<jsp:setProperty name="bean" property="propertyName"
     value="val" />
<jsp:getProperty name="bean" property="propertyName" />

where beanID is the variable name used in the <jsp:usebean/> tag and propertyName is the name of the property you are setting or getting.

To use a variable as either a property or a more often as a value then, between the quotes, enter a JSP expression that returns the value of the variable. For example,

<jsp:setProperty name="employeeBean" property="firstName"
                 value="<%= strFirst %>" />

where strFirst is a variable used in the page.

Example

The following JavaServer Page illustrates how to access a JavaBean from a JavaServer Page. The JavaBean being used here is the one we saw earlier in the item "Example JavaBean".

<HTML>
<HEAD>
<TITLE>Example: Simple Java Bean</TITLE>
<jsp:useBean id="employeeBean" scope="page"
             class="com.acme.mybean.EmployeeBean" />
</HEAD>

<BODY>
<%
  String last = "Burns";
%>

<%-- Set bean properties --%>
<jsp:setProperty name="employeeBean" property="firstName"
      value="Robert" />
<jsp:setProperty name="employeeBean" property="lastName"
      value="<%=last%>" />

<%-- Get bean properties --%>
<P>
<jsp:getProperty name="employeeBean" property="fullName" />
</P>

</BODY>
</HTML>

Note, we can also call the associated property methods directly within Java code using syntax similar to,

employeeBean.setFirstName("Robert");
employeeBean.setLastName("Burns");

String name = employeeBean.getFullName();

JavaBeans and HTML Form Synchronisation

Suppose we have a web application updates records against a database, employee information say. We have an HTML Form to display the employee data and to allow the end user make changes as required. We also have a JavaBean that performs the reading and writing to and from the database. Our JSP code will therefore need to consist of a series of statements that read the HTML form data and then set all the parameters in the JavaBean. Although this isn't complex code it is certainly repetitive.

Form synchronisation is a feature of JSP that synchronises HTML Forms with a JavaBean thus alieviating much of this repetitive coding. More specifically each Form element will synchronise with a property of a JavaBean that has the same name as the Form element.

The following demo illustrates this syncronisation. The scenario here is a JSP page that calculates the interest and repayments made for a bank loan. The two input boxes on the form ask for the amount of the loan and the interest rate. These Form elements synchronise with the equivalent properties in the JavaBean. When this data is submitted the JSP then builds a table of repayment possibilities for 1,2,3,4 and 5 years.

Demo

Syntax

To enable form synchronisation all you need to do is to add the following statement after you declaration of the JavaBean.

  <jsp:setProperty name="BeanName" property="*" />

Where BeanName is the name assigned to the JavaBean in the <usebean/> declaration. Then ensure that the Form elements are named the same as the JavaBean properties remembering case sensitivity.

Summary

JavaBeans are

Quiz

This quiz tests your knowledge of JSP and JavaBeans.

Back Next