In this tutorial, we will see how a JSP interacts with a JavaBean. This helps to easily program and flexibly manage data at the server side.
package person.bean; public class Student implements java.io.Serializable{ private String firstName = null; private String lastName = null; private int age = 0; public Student(){} public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public void setFirstName(String firstName){ this.firstName = firstName; } public void setLastName(String lastName){ this.lastName = lastName; } public void setAge(int age){ this.age = age; } }
<jsp:useBean id="student" class="person.bean.Student"> <jsp:setProperty name="student" property="firstName" value="Zara"/> <jsp:setProperty name="student" property="lastName" value="Ali"/> <jsp:setProperty name="student" property="age" value="10"/> </jsp:useBean> <p>Student First Name: <jsp:getProperty name="student" property="firstName"/> </p> <p>Student Last Name: <jsp:getProperty name="student" property="lastName"/> </p> <p>Student Age: <jsp:getProperty name="student" property="age"/> </p>