GetProperty Tag in JSP

The getProperty tag action is used to retrieve the value of a given property and converts it to a string, and finally inserts it into the output.The getProperty tag action has only two attributes, both of which are required ans simple syntax is as follows:

<jsp:useBean id="myName" ... />
...
<jsp:getProperty name="myName" property="someProperty" .../>

Explanation: As shown above, getProperty tag in JSP has two attributes “name” and “property”. Please note that jsp:getProperty tag must be used with jsp:useBean tag.

  • name Attribute: name attribute in JSP getProperty tag must match with useBean Tag. If JSP has multiple useBean tag, id of the useBean tag is used to identify the bean. While getting property of that bean, id in useBean tag must match with name attribute in getProperty Tag.
  • property Attribute: property attribute in JSP getProperty Tag idnetifies the property whose value is being requested by this tag.

Example-
Employee.java

package com.dineshonjava.bean;

/**
 * @author Dinesh Rajput
 *
 */
public class Employee {
 private String firstName;
 private String lastName;
 private String address;
 
 public String getFirstName() {
  return firstName;
 }
 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }
 public String getLastName() {
  return lastName;
 }
 public void setLastName(String lastName) {
  this.lastName = lastName;
 }
 public String getAddress() {
  return address;
 }
 public void setAddress(String address) {
  this.address = address;
 }
 
}

index.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
 <body>
  <h1>Employee Form Bean Value</h1>
    <jsp:useBean id="emp" class="com.dineshonjava.bean.Employee" scope="page" />
        
    <jsp:setProperty name="emp" property="firstName" value="Dinesh"/>
    <jsp:setProperty name="emp" property="lastName" value="Rajput"/>
    <jsp:setProperty name="emp" property="address" value="Noida"/>
        
     <table>
      <tr>
       <td>First Name</td>
       <td> : </td>
       <td> <jsp:getProperty name="emp" property="firstName"/> </td>
      </tr>
      <tr>
       <td>Last Name</td>
       <td> : </td>
       <td> <jsp:getProperty name="emp" property="lastName"/> </td>
      </tr>
      <tr>
       <td>Address</td>
       <td> : </td>
       <td> <jsp:getProperty name="emp" property="address"/> </td>
      </tr>
     </table>
        
  </body>
</html>

Copy myapp folder on webapp directory of tomcat at
C:Program Files (x86)Apache Software FoundationTomcat 7.0webapps

and restart server and hit the following URL
http://localhost:8080/myapp/index.jsp

Download Example

 

<<Previous <<   || Index ||   >>Next >>
Previous
Next