You are on page 1of 7

What are JavaBeans ?

by Faisal Khan.

Overview
JavaBeans are usual Java classes which adhere to certain coding conventions. Following are the coding conventions I am talking about :

Implements java.io.Serializable interface Provides no argument constructor Provides getter and setter methods for accessing it's properties

Let's now create a simple JavaBean class.

A simple JavaBean class


Create a new SimpleBean.java file and place it in the /WEBINF/classes/com/stardeveloper/bean/test/ folder so that the complete path is :
/WEB-INF/classes/com/stardeveloper/bean/test/SimpleBean.java

Now copy the following code and paste it into the SimpleBean.java file we created above :
package com.stardeveloper.bean.test; public class SimpleBean implements java.io.Serializable { /* Properties */ private String name = null; private int age = 0; /* Empty Constructor */ public SimpleBean() {} /* Getter and Setter Methods */ public String getName() { return name; } public void setName(String s) { name = s; } public int getAge() { return age; } public void setAge(int i) { age = i; }

Explanation
First line is the package statement :
package com.stardeveloper.bean.test;

Next we define our class and make it implement java.io.Serializable interface. Notice that Serializable interface doesn't contain any method. Implementing it just flags to the compiler that we might be serializing this class's objects.
public class SimpleBean implements java.io.Serializable {

Then we declare two variables which hold name and age of a person. These variables inside a JavaBean are called as properties. These properties are private and are thus not directly accessible by other classes. To make them accessible we provide getter and setter methods to get and set their values.
private String name = null; private int age = 0;

Next we create an empty argument constructor. Keep in mind that the only requirement to a JavaBean is an empty "argument" constructor, not that you shouldn't use constructor at all.
public SimpleBean() {}

Explanation
The convention for writing getter and setter methods for JavaBean's properties is really simple. All you have to do is to take the property name e.g. name. Make it's first character uppercase e.g. Name. Now append 'get' for getter method and 'set' for setter method so that it becomes :
public String getName() { return name; } public void setName(String s) { name = s; }

See! how easy it is. Since name variable is of type String, we set the return type of getName() to String. Same is the case with setName() method which takes a parameter of type String because name is of type String. Next we added four getter and setter methods for private variables ( properties ) name and age.
public String getName() { return name; } public void setName(String s) { name = s; } public int getAge() { return age; } public void setAge(int i) { age = i; }

Now close the class.


}

Compiling JavaBean
You will compile JavaBean like you will compile any other Java class file. After compilation, a SimpleBean.class file will be created. We are now done with SimpleBean

Calling JavaBeans from a JSP Page


by Faisal Khan.

Overview
In the earlier article, What are JavaBeans?, we learned what are JavaBeans and we even created a simple JavaBean class file; SimpleBean. We will be using this SimpleBean class in this tutorial, so if you haven't read above article then I suggest you do it now.

Before we continue to describe how to code a JSP page to call that JavaBean, let's first discuss the three JSP tags provided to us to make use of JavaBeans.

JSP JavaBean Tags


Following are the three tags :

<jsp:useBean> <jsp:setProperty> <jsp:getProperty>

Let us now study them one by one.

i. <jsp:useBean>
This tag is used to declare and instantiate the JavaBean class. It's syntax is as follows :
<jsp:useBean id="object-name" scope="page | request | session | application" type="type-of-object" class="fully-qualified-classname" beanName="fully-qualified-beanName" />

Let us now see what are the different attributes :

id - name of the object e.g.


String name = null;

In the above code, name is the 'id' we are talking about.

scope - an optional attribute by which you can control when your JavaBean object will be destroyed. Default is page, which means every page view will create a new JavaBean. type - type of the object which can be the same class, a super class or an interface which the class implements. This parameter is optional. e.g.
String name = "Faisal Khan";

In the above code, String is the 'type' we are talking about.

class - a fully qualified class name e.g.


Date d = new java.util.Date();

In the above code, java.util.Date is the 'class' we are talking about.

beanName - it is also fully qualified class name just like 'class' attribute above. Only difference is that the class name in the case of 'beanName' can be provided at request time.

ii. <jsp:setProperty>
This tag is used to set the value of one or all the properties of given JavaBean. It's syntax is as follows :
<jsp:setProperty name="id-of-the-JavaBean" property="name-of-property" param="name-of-request-parameter-to-use" value="new-value-of-this-property" />

Let us now see what the different attributes mean :


name - 'id' of the <jsp:useBean> tag you set above. property - name of the property whose value you want to set. param - name of the request paramter you want to use to set the value of this property. value - the new value you want to set for this property.

iii. <jsp:getProperty>
This tag is used to retrieve the value of a given property from the given JavaBean. It's syntax is as follows :
<jsp:getProperty name="name-of-the-object" property="name-of-property" />

Let us see what are the different attributes for this tag :

name - 'id' of the <jsp:useBean> tag we set above. property - name of the property whose value you want to retrieve.

We are now finished studying the JSP tags provided to manipulate JavaBeans. Let's just spend a few minutes studying what is scope attribute we discussed in <jsp:useBean> tag.

Object Scope
Every JavaBean class object or any other class object we create will have a scope. Scope means the length of time this object will remain in memory. There are four kinds of scopes :

page - it means a new object will be created and destroyed for every page view. This is the default for <jsp:useBean> tag when you don't explicitly give it any. request - it means the newly created object will be created and bound to the request object. It is used in different JSP architectures we will study later when we have covered all the basic topics of JSP pages. session - the newly created object will be bound to the session object. What this means is that every visitor coming to your site will have a separate session for it, so you will not have to create a new object every time for it. You can just retrieve that object later again from the session object when you want it. application - an object bound to application object means that your object will stay as long as the application remains loaded. This can be useful when for instance you want to count page views or daily sessions for your site. To learn more about using sessions in Java Servlets, please read Managing Sessions with Java Servlets.

Ok, we have now studies all the three tags for JavaBeans manipulation and also studied briefly what are the different object scopes and what they mean to us. Let us now move forward on the next page where we code our SimpleBean.jsp JSP page which will call this JavaBean, set new values for it's parameters and then display the values of these parameters to the user.

SimpleBean.jsp JSP page


Create a new SimpleBean.jsp page and place it in the /WEB-APP folder. WEB-APP is the complete path to your web application e.g. C:\yoursite, then place SimpleBean.jsp page in C:\yoursite\SimpleBean.jsp. Remember, never put JSP pages in /WEBINF/ folder.

Now copy the following code and paste it into the SimpleBean.jsp page above :
<html> <head> </head> <body>

<title>SimpleBean Test Page</title>

<%-- Creating JavaBeans --%> <jsp:useBean id="simple" class="com.stardeveloper.bean.test.SimpleBean"> <jsp:setProperty name="simple" property="name" value="Faisal Khan" /> <jsp:setProperty name="simple" property="age" value="24" /> </jsp:useBean> <%-- Displaying JavaBean property's value --%> <p>Name retrieved from JavaBean has the value of : <b><jsp:getProperty name="simple" property="name" /></b>.<br> Age retrieved from JavaBean has the value of : <b><jsp:getProperty name="simple" property="age" /></b> years.<br> </p> </body> </html>

Explanation
Above code is a simple JSP page which makes use of all the three JavaBean tags we discussed earlier. First we create a new JavaBean object for our SimpleBean class by using <jsp:useBean> tag :
<jsp:useBean id="simple" class="com.stardeveloper.bean.test.SimpleBean">

Then we use <jsp:setProperty> tag to set different values for both the name and age properties of our SimpleBean class.
<jsp:setProperty name="simple" property="name" value="Faisal Khan" /> <jsp:setProperty name="simple" property="age" value="24" />

We then close the <jsp:useBean> tag.


</jsp:useBean>

Next we display the values of these properties using the <jsp:getProperty> tag twice.
<jsp:getProperty name="simple" property="name" /></b>.<br> <jsp:getProperty name="simple" property="age" /></b>.<br>

You might also like