JSP bean get property

The code illustrate an example from JSP bean get property. In this example we define a package bean include a class Employees.

JSP bean get property

JSP bean get property

        

The code illustrate an example from JSP bean get property. In this example we define a package bean include a class Employees. Inside the class we declared a String variable first Name, last Name and address. The get Address, first Name, last Name return you the value from a bean in JSP page. The JSP page uses bean get property and return the value stored in it.

<jsp:useBean>

  1. The <jsp:useBean> is used to instantiates a java bean component. 
  2. An attempt is made to locate and search an instance of the bean.

Attributes used in <jsp:useBean>

  1.  Id  - An attempt is made to search the bean with the scope .
  2.  class - This indicate package. class is used to give the reference of the bean class.

<jsp:get Property> -  

The <jsp:useBean>element contains a <jsp:getProperty> element that is used to retrieve the value stored in the getter method.

In order to run this program we need to place the file inside the Tomcat Home\WebApps\jsp bean get Property and start your tomcat. Once Tomcat is started ,type the url browser and run your application.

jsp bean getproperty.jsp

<html>
 <body>
  <h1>Get Value from bean</h1>
   <jsp:useBean id="emp" class="bean.Employees"/>
    <table>
     <tr>
      <td>First Name :
       <jsp:getProperty name="emp" property="firstName"/> 
      </td>
     </tr>
     <tr>
      <td>Last Name : 
       <jsp:getProperty name="emp" property="lastName"/> 
      </td>
      </tr>
     <tr>
      <td>Address :
       <jsp:getProperty name="emp" property="address"/> 
      </td>
     </tr>
    </table>
 </body>
</html>
 
Employees.java
package bean;
public class Employees {
    protected String firstName;
    protected String lastName;
    protected String address;
    public String getAddress() {
        address = "Delhi";
        return address;
    }
    public String getLastName() {
        lastName = "Singh";
        return lastName;
    }
    public String getFirstName() {
        firstName = "Komal";
        return firstName;
    }
}

Output of the program

Download Source code