Home

JSP and Beyond

a pragmatic primer on building web-based solutions with Java technologies


Accessing a Bean

Beans can be accessed in a variety of ways. Once again, the beauty of using a bean is that an HTML developer can actually call a bean into their JSP page without having to know how to code Java, and yet can still access the rich functionality provided by the JSP. To provide this level of abstraction, JavaBeans have three ways in which you can interact with them through JSP which are outlined below through the use of XML tags.

1. In order to use a JavaBean it must be instantiated on the JSP page. This is done using the following code block:

<jsp:useBean id=”mynewbean” class=”com.yourclassname” scope=”request”>

Using this line of code, the container is smart enough to see if the object already exists and if not creates a new one with the attributes provided.

2. Setting a value for a bean is done through the setProperty action. Whatever code exists within the method for this attribute will execute upon this set.

<jsp:setProperty name=”newsfeed” property=”topic” value=”weather”/>

3. Getting content from a bean. Whatever code exists within the method for this attribute will execute upon this set.

<jsp:useBean id=”calendar” scope=”page” class=”com.mycompany.Calendar” />
<h2>
Calendar of <jsp:getProperty name=”calendar” property=”username” />
</h2>

Beans can also be accessed through scriptlets, just as though you were directly using a method from a class. It is not a good practice to return HTML markup from a bean. If development of tabled structures for lists or other presentation of collections is needed, it is best practice to return a collection or record set from the bean and deal with displaying the contents through the use of additional scriptlet markup or JSTL. The example below outlines one such approach.

<%@ page contentType=”text/html” %>
<%@ taglib prefix=”c” uri=”http://java.sun.com/jstl/core_rt” %>

<%
// Popultate an array with the values from our bean for the customer listing
java.util.ArrayList alCustomers = JSPBeyond.CustomerBean.GetCustomerRecords();

// Set the “CustomerListing” attribute so it can hold the array for access later within this page
request.setAttribute(”CustomerListing”, alCustomers);
%>

<html>
<head>
<title>JSP and Beyond - Customer List - Bean Example</title>
</head>
<body>

<h1>Customer List - Bean and JSTL Example</h1>

<table width=”100%” border=”1″>
<tr>
<td colspan=”4″>Customer</td>
</tr>

<%– Start a loop through the results from the SQL query and mix it with HTML –%>
<c:forEach var=”row” items=”${CustomerListing}”>
<tr>
<%– Notice how with each “row” object we can access the column name by specifying it after the “row” object –%>
<td><a href=”CustomerList-Bean.jsp?shownotes=<c:out value=”${row.customerID}”/>”>show notes</a> <c:out value=”${row.customerName}”/></td>
<td><a href=”CustomerEdit-Bean.jsp?CustomerID=<c:out value=”${row.customerID}”/>”>edit</a></td>
<td><a href=”NoteAdd-Bean.jsp?CustomerID=<c:out value=”${row.customerID}”/>”>add note</a></td>
<td><a href=”CustomerDelete-Bean.jsp?CustomerID=<c:out value=”${row.customerID}”/>”>delete</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>

Please note that the following code would generally exist in a Servlet and then be sent to the JSP if you were using an MVC framework, by using the request.setAttribute code as shown below.

// Popultate an array with the values from our bean for the customer listing
java.util.ArrayList alCustomers = JSPBeyond.CustomerBean.GetCustomerRecords();

// Set the “CustomerListing” attribute so it can hold the array for access later within this page
request.setAttribute(”CustomerListing”, alCustomers);

The code would remain the same as the one in our scriptlet. This approach is detailed toward the end of the book.