JavaBeans


A JavaBean class is a special java class with the following characteristics:

  1. It should have a no-argument constructor.
  2. It should implement the java.io.Serializable interface.
  3. It may have a number of properties which can be read or written using getter or setter methods.

Example: The following is an example of a java bean class of a college.

Student.java

package college;
public class Student implements java.io.Serializable
{
    private String name;
    private int rollNo;
    public Student()
    {
        this.name = “”;
        this.rollNo = 0;
    }
    public void setName (String name)
    {
        this.name = name;
    }
    public String getName ()
    {
        return name;
    }
    public void setRollNo (int rollNo)
    {
        this.rollNo = rollNo;
    }
    public int getRollNo ()
    {
        return rollNo;
    }
}


Teacher.java

package college;
public class Teacher
{
    public static void main(String args[])
    {
        Student s = new Student();
        s.setName(“Abhishek”);
        s.setRollNo(1027);
        System.out.print(“Name is : ” + s.getName() + “\n Roll no is : ” + s.getRollNo());
    }
}



jsp:useBean action tag

It is used to declare and initialize the bean object.

If a bean object of the Bean class is already created then it doesn't create a new bean.

Syntax:

    <jsp:useBean id = “beanName” class = “package.className” scope = “page | request | session | application” type = “package.className” beanName = “beanName” >
    </jsp:useBean>    


Attribute Usage
id Used to identify the bean.
Class Used to create an object of the bean class.
Scope Used to represent the scope of the bean. It may be page, request, session or application. Default scope is page.
Type Used to provide a data type to the bean.
beanName Creates the bean using java.beans.Beans.instantiate() method.

Example:

index.jsp

    <jsp:useBean id=”sqr” class=”com.tutorialsduniya.Calculate” />
    <% int s = sqr.square(17);
        out.print(“Square of 17 is : “ + s);
    %>


Calculate.java

    package com.tutorialsduniya;
    public class Calculate
    {
        public int square (int n)
        {
            return n*n;
        }
    }



jsp:getProperty action tag

It is used to get/retrieve the value of a property from the JavaBean object.

Syntax:

    <jsp:getProperty name=”beanName” property=”propertyName” />


Attribute Usage
name Used to represent the name of the JavaBean object.
property Used to represent the property of the JavaBean object.

Example: The following example will print the value of the property.

StudentBean.java

import java.io.Serializable;
public class StudentBean implements serializable
{
    private String name;
    public StudentBean()
    {
        this.name=””;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public String getName()
    {
        return name;
    }
}


getPropertyExample.jsp

    <html>
    <body>
        <jsp:useBean id=”student” class=”StudentBean” />
        Name of the student is : <jsp:getProperty name=”student” property=”name” />
    </body>
    </html>



jsp:setProperty action tag

It is used to set the value of a property to the JavaBean object.

Syntax:

    <jsp:setProperty name=”beanName” property=”propertyName” value=”propertyValue” />


Attribute Usage
name Used to represent the name of the JavaBean object.
property Used to specify which property of the bean to access.
value Used to represent the value to be provided to the bean.

Example: The following example will set the value of the ‘name’ property as ‘Abhishek’.

StudentBean.java

    import java.io.Serializable;
    public class StudentBean implements serializable
    {
        private String name;
        public StudentBean()
        {
            this.name=””;
        }
        public void setName(String name)
        {
            this.name = name;
        }
        public String getName()
        {
            return name;
        }
    }


setPropertyExample.jsp

    <html>
    <body>
        <jsp:useBean id=”student” class=”StudentBean” />
        <jsp:setProperty name=”student” property=”name” value=”Abhishek” />
        Name of the student is : <jsp:getProperty name=”student” property=”name” />
    </body>
    </html>