You can use JSP action element <jsp:setProperty> to set the value of a property/instance variable already defined in a class.
In order to set 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:setProperty>
This is a syntax of <jsp:setProperty> action element with its attributes.
<jsp:setProperty
id = "nameOfExistingJavaBean"
property = "propertyName"
value = "StringLiteral | <%= expression>"
/>
Let's discuss the attributes of <jsp:setProperty> action element -
id
The id attribute points to the name of an existing Java Bean which is already created using <jsp:useBean> action element.
property
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.
value
The value attribute points to value which will be passed to method defined in the class, to set the value of an existing property/instance variable 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 which can be set using method setMessage() and it's value can be retrieved using
getMessage defined with a value, as shown below.
A.java
package decodeJava;
public class A
{
//Constructor
public A()
{
System.out.println("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()
{
return message;
}
//Setting the bean property named message
public void setMessage(String str)
{
message = str;
}
}
Make sure that you store the compiled form(.class) of this class in "WEB-INF/Classes" folder within a new directory of Tomcat's webapps
folder, or within Root folder of Tomcat installation.
Advertisement
Using <jsp:setProperty> 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 and set the value of its property named message defined in the same class, using <jsp:getProperty> action and <jsp:setProperty> action.
SetProperty.jsp
<html>
<head>
<title>Using JSP getProperty action</title>
</head>
<body>
<jsp:useBean id="MyBean" class = "decodeJava.A" />
<b>Getting the value of property named "message" of class A </b>
<br/>
<br/>
<b>Property name :</b> message
<br/>
<b>Property value:</b> <jsp:getProperty name ="MyBean" property="message" />
<br/>
<br/>
<b>Setting the value of property named "message" of class A</b>
<br/>
<br/>
<b>Property name :</b> message <br/>
<b>Property value:</b> Have a good day!
<jsp:setProperty name ="MyBean" property="message" value="Have a good day!" />
<br/>
<br/>
<b>Getting the value of property named "message" of class A</b>
<br/>
<br/>
<b>Property name :</b> message <br/>
<b>Property value:</b> <jsp:getProperty name ="MyBean" property="message" />
</body>
</html>
Executing SetProperty.jsp gives us value of the property named message which was initially defined in the class A, using JSP <jsp:getProperty> action element.
Next, we have used JSP <jsp:setProperty> action element to set the value of the same property named message.
Finally, we have again retrieved the value of property named message, just to see it's new value.