JSP <jsp:useBean> Action

Java Bean :

In this tutorial we will see how to use a bean class in JSP with the help of jsp:useBean, jsp:setProperty and jsp:getProperty action tags.

StudentBean.java

package com.c4learn;
public class StudentBean {
   private int roll;
   private String name;
   private String city;
   private int age;
   public int getRoll() {
      return roll;
   }
   public void setRoll(int roll) {
      this.roll = roll;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public String getCity() {
      return city;
   }
   public void setCity(String city) {
      this.city = city;
   }
   public int getAge() {
      return age;
   }
   public void setAge(int age) {
      this.age = age;
   }
}

index.jsp

<html>
<head>
<title>UseBean Example</title>
</head>
<form action="output.jsp" method="post">
   Roll : <input type="text" name="roll"> <br> 
   Name : <input type="text" name="name"> <br> 
   City : <input type="text" name="city"> <br> 
   Age  : <input type="text" name="age">  <br> 
   <input type="submit" value="Submit"> 
</form> 
</html>

output.jsp :

<jsp:useBean id="info" class="com.c4learn.StudentBean"></jsp:useBean> 
<jsp:setProperty property="*" name="userinfo"/>
Student Details are :
<jsp:getProperty property="roll" name="info"/><br> 
<jsp:getProperty property="name" name="info"/><br> 
<jsp:getProperty property="city" name="info"/><br> 
<jsp:getProperty property="age"  name="info" /><br>

Explanation of Code :

In the above code we have firstly written the simple class with instance variables like this -

package com.c4learn;
public class StudentBean {
   private int roll;
   private String name;
   private String city;
   private int age;
}

Now right click in the code window and select the option “Source” ==> “Generate Getters and Setters



After the generation of the getter setter methods we need to write the input.jsp file which will accept the data from the user.

<form action="output.jsp" method="post">
   Roll : <input type="text" name="roll"> <br> 
   Name : <input type="text" name="name"> <br> 
   City : <input type="text" name="city"> <br> 
   Age  : <input type="text" name="age">  <br> 
   <input type="submit" value="Submit"> 
</form>

In the above form we have used the name attribute of input elements in such as way that it matches with the instance variables used in the StudentBean.java class.

Inside the output.jsp file we have used the StudentBean. useBean action is used to initialize the class.

Attribute Explanation
id Unique name to identify the bean
class fully qualified name of the class
<jsp:useBean id="info" class="com.c4learn.StudentBean"></jsp:useBean>

Using below statement, we have mapped the properties of bean class and JSP using setProperty action tag

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

‘*’ in the property field is used to map the values based on their names.We have already used the same property name in bean class and index.jsp JSP.

<jsp:getProperty property="roll" name="info"/>
<jsp:getProperty property="name" name="info"/> 
<jsp:getProperty property="city" name="info"/> 
<jsp:getProperty property="age"  name="info"/>

using the getProperty() we retrieve the value of a given property and converts it to a string.