Using JSP Beans with example

The following example illustrates using JSP beans. This will introduce you to three tags

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

Before we start, let us talk about why use these beans.

Introduction to Beans

A bean is a class containing some member variables and methods. This class represents a particular entity. For example, take Student class. A student typically contains a number, name and age. So, i will create a class called Student which contains the member variables sno, sname, age.

Now I want to store those student details into the database one by one. So every time a student record is entered, i assign those values into sno, sname, age correspondingly. And then i’ll store them in the database.
So, every time data is entered, I’ll store the details in the class member variables and then get them and store in the database.

This is a clean approach. This is what is called as re-usability which means that for every student record we are using the same class.

A bean class contains typically of two types of methods called setter and getter methods.
A setter method is used to put the value into a member variable.
A g_etter method_ is used to get the value of a member variable.

There are some rules for giving names to these setter and getter methods. These rules are kept because the JVM has to understand what is what.

For example, if a member variable is named sno then, the corresponding methods will be:

public void setSno(int sno)
{
	this.sno=sno;
} 
public int getSno()
{
	return sno;
} 

Note that the names of those methods must be the same i.e. they should start with set or get and then followed by the member variable name with first letter capital. The parameter name, can however be different.

<jsp:useBean>
This tag typically takes two attributes namely, id and class.

class: This is the bean class. Here Student
id: This is the name of the object for the student class.
scope: This tag takes the scope of the object session, request, page etc.

Syntax:

<jsp:useBean id="st" class="beans.student" />

When this tag is simply closed as above, the default constructor of the Student class is activated

<jsp:setProperty>
This tag is used to inject the values into the object. It takes two to three parameters.

name: The name of the object to which the values must be injected.
property: The member variable to which the value must be injected
value: This is the value to be injected (set) into the member variable. Usually, we don’t do this, the value is taken from the form.
param: This is the name of the input field from which the value is to be taken. When the input field names and the property names in the bean class are different we use this. Otherwise, we specify * in the property field when all the property names and the input field names are same.

<jsp:getProperty>
This tag is used to get the injected value from the object.
name: The name of the object.
property: The member variable.

Code

Folder structure

webapps
  |_ jsp6
	 |_ WEB-INF
		  |_ classes
				  |_ beans
						|_ Student.java
						|_ Student.class
		 |_ index.jsp
		 |_ print.jsp

Student.java

package beans;
public class Student
{
private int sno;
private String sname;
private int age;

    public void setSno(int sno)
    {
    this.sno=sno;
    }
    
    public int getSno()
    {
    return sno;
    }

    public void setSname(String sname)
    {
    this.sname=sname;
    }

    public String getSname()
    {
    return sname;
    }

    public void setAge(int age)
    {
    this.age=age;
    }

    public int getAge()
    {
    return age;
    }
}

index.jsp

<html>
    <body>
    <form action="print.jsp">
Student number: <input type="text" name="sno"/>
Student name: <input type="text" name="sname"/>
Student age: <input type="text" name="age"/>
    <input type="submit" value="Submit"/>
    </form>
    </body>
</html>

Here the data is sent to print.jsp using GET (default).

print.jsp

<%--
Create an object 'st' for the class beans.Student using
default constructor
--%>

<jsp:useBean id="st" class="beans.Student">


<%--
Put the values entered into the form into the bean class
--%>

<jsp:setProperty name="st" property="*"/>


<%--
Get all the values that are put in the object 'st' with the
given property names
--%>

<jsp:getProperty property="sno" name="st"/>
<jsp:getProperty property="sname" name="st"/>
<jsp:getProperty property="age" name="st"/>

</jsp:useBean>

ధన్యవాదాలు (Thanks)

No comments: