JSP useBean, getProperty and setProperty tags

Java bean is a special type of Java class that contains private variables and public setter and getter methods. Java bean class implements Serializable interface and provides default constructor.

JSP allows you to access the Java beans in JSP files. To invoke Java bean, useBean action tag is used and to access the properties of Java beans, JSP setter and getter method is used.

Example of useBean Tag

In this example, setproperty() and getproperty() method is used to set and get data of current Java class.

EmployeeBean.java

package com.tutorialandexample;
import java.io.Serializable;
public class EmployeeBean implements Serializable {
    private String name;
    private int id;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    } 
}

index.jsp

<html>
<head>
<title>Tutorial and Example</title>
</head>
<body>
<jsp:useBean id="emp" class="com.tutorialandexample.EmployeeBean">
<jsp:setProperty property="name" name="emp" value="Sushant Singh"/>
<jsp:setProperty property="id" name="emp" value="101"/>
<jsp:setProperty property="age" name="emp" value="23"/>
</jsp:useBean>
Employee ID : <jsp:getProperty property="id" name="emp"/> <br>
Employee Name : <jsp:getProperty property="name" name="emp"/> <br>
Employee Age : <jsp:getProperty property="age" name="emp"/>
</body>
</html>

Output: