You can use JSP action element, <jsp:getProperty>, to retrieve the value of a property/instance variable which is already defined in a class.
In order to retrieve the value of a property defined in a class,
we will first have to access an object(bean) of a class by using its id, which is already created by using the <jsp:useBean> action element.
Syntax of <jsp:getProperty>
This is a syntax of <jsp:getProperty> action element with its attributes.
<jsp:getProperty name = "nameOfExistingJavaBean" property = "propertyName" />
Let's discuss the attributes of <jsp:getProperty> action element:
The name attribute points to the name of an existing Java Bean which is already created by using the <jsp:useBean> action element.
The property attribute points to an existing property/instance variable defined in a class. This attribute is given the
same value as the name of the existing property/instance variables defined in a class.
Creating a simple class in Java
Now we are going to create a Java class, A.java which has a property/instance variables named message, defined with a value, as shown below.
public class A
//Creating a bean property named "message"
private String message = "Hello from A class";
//Getting the value of bean property named "message"
public String getMessage()
Make sure that you store the compiled form(.class) of this class in "WEB-INF/Classes" folder of your project or within your
project directory in Tomcat's webapps
folder, or within the Root folder of Tomcat installation.
Using <jsp:getProperty> action
We are going to create an object(bean) of the class A.java by using the <jsp:useBean> action element and then we will refer to the same object(bean)
by using its id(already created)
and retrieve the value of its property named message defined in the same class.
<title>Using JSP getProperty action</title>
<jsp:useBean id="MyBean" class = "decodeJava.A" />
<b>Getting the value of property named "message" of class A </b>
<b>Property name :</b> message
<b>Property value:</b> <jsp:getProperty name ="MyBean" property="message" />
Executing the GetProperty.jsp gives us value of the property named message, defined in the class A, by using the <jsp:getProperty> action element of JSP, as shown below.