A sample that shows Java Beans, Servlets and JSP working together

This tutorial shows the use of Java Beans, JSP and Servlet and how they work together. As an added bonus this tutorial also explains the use of nested properties of Java Beans using EL. The EL provides you with a simple mechanism to access nested properties of a JavaBean. For example, Listing below shows a JavaBean, which has a nested property of type Address

package com.apress.projsp;
public class Person {
private String name;
private int age;
private Address address;
public Person() {
setName("A N Other");
this.address = new Address();
public void setName(String name) {
this.name = name;
public String getName() {
return name;
public void setAge(int age) {
this.age = age;
public int getAge() {
return age;
public void setAddress(Address address) {
this.address = address;
public Address getAddress() {
return address;

As you can see, this JavaBean has a property that is in fact another JavaBean—the Address JavaBean (Address.java). Listing below shows the Address JavaBean.

// Address.java
package com.apress.projsp;
import java.util.Collection;
public class Address {
private String line1;
private String town;
private String county;
private String postcode;
private Collection phoneNumbers;
public Address() {
this.line1 = "line1";
this.town = "a town2";
this.county = "a county";
this.postcode = "postcode";
public void setLine1(String line1) {
this.line1 = line1;
public String getLine1() {
return line1;
public void setTown(String town) {
this.town = town;
public String getTown() {
return town;
public void setCounty(String county) {
this.county = county;
public String getCounty() {
return county;
public void setPostcode(String postcode) {
this.postcode = postcode;
public String getPostcode() {
return postcode;
public Collection getPhoneNumbers() {
return phoneNumbers;
public void setPhoneNumbers(Collection phoneNumbers) {
this.phoneNumbers = phoneNumbers;

It’s simple to access these nested properties by using the EL. With the EL, you can chain the various objects and properties in an EL statement. The following JSP snippet shows how you could use chaining to access the line1 property of the address property of the person JavaBean.


The Address JavaBean contains a collection of other JavaBeans as one of its properties. Although you can’t see it in Listing above this collection is a collection of JavaBeans—PhoneNumber JavaBeans. This JavaBean is shown in Listing below.

// PhoneNumber.java
package com.apress.projsp;
public class PhoneNumber {
private String std;
private String number;
public String getNumber() {
return number;
public String getStd() {
return std;
public void setNumber(String number) {
this.number = number;
public void setStd(String std) {
this.std = std;

The EL also provides a simple mechanism for accessing such collections and the properties of their enclosed JavaBeans. The following JSP snippet would access the first phone number for a person’s address:


As this snippet shows, you can freely mix both dot and bracket notation as needed to access JavaBean properties. 

We can bring this whole discussion together by way of the following example. Listing below shows a JSP page that displays all the details relating to a person and that person’s address. Note how the JSP page uses alternative syntaxes for object property access.

// complexBean.jsp
<title>EL and Complex JavaBeans</title>
body, td {font-family:verdana;font-size:10pt;}
<h2>EL and Complex JavaBeans</h2>
<table border="1">

Unlike previous examples where you loaded the JSP page directly, this example uses a simple Java servlet  to set up the information within the JavaBeans. The servlet then adds the object to the request by using the name “person.” The JSP page, as mentioned earlier, searches the various scopes to find the JavaBean with the name used in the EL statement. Listing below, PopulateServlet.java, is the servlet that initializes the various JavaBeans and then uses a RequestDispatcher to forward the request to complexBean.jsp. To compile this class, you’ll need to include a servlet library in the CLASSPATH. If you are using Tomcat 5, you can find the library, servlet-api.jar, in the common\lib directory. If you are using some other web container, check your container documentation for the correct servlet library to include.

// PopulateServlet.java
package com.apress.projsp;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class PopulateServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
Person p = new Person();
p.setName("Sam Dalton");
Address a = new Address();
a.setLine1("221b Baker Street");
a.setCounty("Greater London");
a.setPostcode("NW1 1AA");
ArrayList al = new ArrayList();
PhoneNumber ph = new PhoneNumber();
ph = new PhoneNumber();
req.setAttribute("person", p);
RequestDispatcher rd = req.getRequestDispatcher("complexBean.jsp");
rd.forward(req, res);

This servlet class should be placed within the WEB-INF\classes\com\apress\projsp folder. You’ll also need to modify the WEB-INF\web.xml file to contain the additional tags in Listing below.

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation= ➥

Compile all the Java classes and deploy the files to a web container in the manner described for previous examples. The request for this application is http://localhost:8080/expressionLanguage/BeanTest.  The request is passed to the BeanTestServlet. The servlet creates and initializes the various JavaBeans and forwards the request to complexBean.jsp. 

Figure: shows the web page you would see in your browser.

The expression language can easily handle nested properties of JavaBeans. This example shows how an object or attribute can be created in one part of the web application and accessed from some other part of the application. The JSP page searches the various scopes until it locates the named object.

COPYRIGHT © 2017 java-samples.com. ALL RIGHTS RESERVED.