JSP Bean Servlet
JSP Bean Servlet
java
package com.bms.mca;
public class Person {
private String name;
private int age;
private Address address;
public Person() {
setName("A N Other");
setAge(21);
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;
}
}
package com.bms.mca;
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.
${person.address.line1}
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.bms.mca;
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:
${person.address.phoneNumbers[1].number}
As this snippet shows, you can freely mix both dot and bracket notation as needed to access
JavaBean properties.
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
<html>
<head>
<title>Java Bean with Servlet and JSP </title>
<style>
body, td {font-family:verdana;font-size:10pt;}
</style>
</head>
<body>
<h2> Complex JavaBeans</h2>
<table border="1">
<tr>
<td>${person.name}</td>
<td>${person.age}</td>
<td>${person["address"].line1}</td>
<td>${person["address"].town}</td>
<td>${person.address.phoneNumbers[0].std}
${person.address.phoneNumbers[0].number}</td>
<td>${person.address.phoneNumbers[1].std}
${person.address.phoneNumbers[1].number}</td>
</tr>
</table>
</body>
</html>
Listing below, PopulateServlet.java, is the servlet that initializes the various JavaBeans and
then uses a RequestDispatcher to forward the request to complexBean.jsp.
// PopulateServlet.java
package com.bms.mca;
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("Sairam");
p.setAge(62);
Address a = new Address();
a.setLine1("All in One Street");
a.setTown("Shirdi");
a.setCounty("India");
a.setPostcode("100001");
ArrayList al = new ArrayList();
PhoneNumber ph = new PhoneNumber();
ph.setStd("01895");
ph.setStd("678901");
al.add(ph);
ph = new PhoneNumber();
ph.setStd("0208");
ph.setStd("8654789");
al.add(ph);
a.setPhoneNumbers(al);
p.setAddress(a);
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\bms\mca folder. You’ll
also need to modify the WEB-INF\web.xml file to contain the additional tags in Listing below.
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation= ➥
"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>BeanTestServlet</servlet-name>
<servlet-class>com.apress.projsp.PopulateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BeanTestServlet</servlet-name>
<url-pattern>/BeanTest</url-pattern>
</servlet-mapping>
</web-app>
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.