Home

JSP and Beyond

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


What are JavaBeans?

It might sound strange, but there is nothing inherently special about JavaBeans. They are just blocks of Java code that follow a few standards. That being said, they are very useful.

Beans are a great way of representing an item such as a user or product. You can think of a bean as a bucket that you can place information into, take information out of or let it know that you need it to do some sort of operation and send you the result when it is done. The beauty is that when you interact with this bucket, you do not need to know anything regarding how it does its job because the inner workings are abstracted by “Getter” and “Setter” methods that broker access to the attributes of the bean.

“Getter” and “Setter” methods are created by adding “get” or “set” to the name of an attribute within the bean that you would like to interact with. For example, the attribute employeeFirstName would have a setter called setEmployeeFirstName.

Using beans within JSPs can help you separate the presentation (HTML) from the business logic. Leveraging this type of encapsulation is much better than writing Java code directly into your JSPs. Beans will make code easier to edit and maintain. They will also allow for reusability across any number of JSPs which will save you coding and testing time.

As we embrace more advanced features within the Java development world, beans will become even more important to the development. More advanced development frameworks like Struts and Java Server Faces use beans to maintain the state between requests, and pass data between presentation and business logic layers.

Although beans can be used directly within a JSP, they are generally sent to the JSP from a Servlet in a MVC type of framework. This chapter will not detail with that, but the MVC section of our sample application toward the end of the book will demonstrate this interaction. For the purposes of this chapter, we will only cover the basics by using a simple bean like the one outlined below.

The above bean contains a few components. The “attributes” hold the person’s first name, last name, age and email address. It allows developers to access these attributes and set all of them, with the exception of the person’s age, through “getters” and “setters”. This is a nice benefit of a bean in that it can be designed to allow updates to only certain portions of the bean’s contents. In addition to these attributes and “getters” and “setters”, the bean also provides two methods, one to calculate pay for the employee and the other to give the employee a pay raise. To put the above example into the context of a Java class you can review the code for this bean below.

SamplePersonBean.java

package JSPBeyond;
public class SamplePersonBean {

private String firstName;
private String lastName;
private String emailAddress;
private int age;

public SamplePersonBean() {
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getEmailAddress() {
return emailAddress;
}

public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}

public int getAge() {
return age;
}

public void givePayRaise ()
{
// Add code here to give more $!
}

public int calculatePay ()
{
// Figure out what the employee’s pay and then send back the result
Return 1;
}

}