You can use JSP action element <jsp:getProperty> to retrieve the value of a property/instance variable already defined in a class.
In order to retrieve the value of a property defined in the class,
we will first have to access the object(bean) of a class using its id already created using <jsp:useBean> action element.
Syntax of <jsp:getProperty>
This is a syntax of <jsp:getProperty> action element with its attributes.
<jsp:getProperty id = "nameOfExistingJavaBean" property = "propertyName" />
Let's discuss the attributes of <jsp:getProperty> action element -
The id attribute points to the name of an existing Java Bean which is already created using <jsp:useBean> action element.
The property attribute points to an existing property/instance variable defined in the class. This attribute is given the
same value as the name of the existing property/instance variables defined in the class.
Creating a simple class in Java
Now we are going to show to how to create an object(bean) of class in java using
<jsp:useBean> action. Let's say this Java class is A.java and it 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 Root folder of Tomcat installation.
Using <jsp:getProperty> action
We are going to create the object(bean) of the class A.java using <jsp:useBean> action and then we will refer to the same object(bean)
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 GetProperty.jsp gives us value of the property named message defined in the class A, using JSP <jsp:getProperty> action element, as shown below.