JSP setProperty action tag

In this post, you will learn:

  • What is the role of setProperty action in JSP?
  • How setProperty action is used in JSP?

setProperty action of JSP:

In the last post, you were told that external objects can be used in a JSP page. If an external object have some properties then they need to be set before using the object. <jsp:setProperty /> action is used to set the properties of such external objects.

This action can be used in the following ways:

1. To directly set the value of a bean property, following syntax is used:

<jsp:setProperty name=”beanName” property=”propertyName” value=”propertyValue” />

let following be an external object in a JSP page.

<jsp:useBean id=”user” class=”com.techmentro.learningpad.User” scope=”session” />

To set a@b.com as the value of mailId property of the user bean, following action is required.

<jsp:setProperty name=”user” property=”mailId” value=”a@b.com” />

Following code will be generated in the _jspService() method of the auto generated servlet:

//user object will be locked for thread safety
synchronized(user)
{
//MailId property value is set by invoking setMailId() method.
user.setMailId("a@b.com");
}

2. To set the value of a bean property using a request parameter, following syntax is used:

<jsp:setProperty name=”beanName” property=”propertyName” param=”parameterName” />

To set the mailId property of the user bean using email request parameter, following action is required.

<jsp:setProperty name=”user” property=”mailId” param=”email” />

Following code will be generated in the _jspService() method of the auto generated servlet:

//user object will be locked for thread safety
synchronized(user)
{
//MailId property value is set using the value of email request parameter.
user.setMailId(request.getParameter("email"));
}

3. To set a property value using a request parameter of the same name, following syntax is used:

<jsp:setProperty name=”beanName” property=”propertyName” />

To set the mailId property of the user bean using mailId request parameter, following action is required:

<jsp:setProperty name=”user” property=”mailId” />

Following code will be generated in the _jspService() method of the auto generated servlet:

//user object will be locked for thread safety
synchronized(user)
{
//MailId property value is set using the value of mailId request parameter.
user.setMailId(request.getParameter("mailId"));
}

4. To set the value of all the properties of a bean using the request parameters of the same name, following syntax is used:

<jsp:setProperty name=”beanName” property=”*” />

To set the name, mailId and password properties using the request parameters of the same name, following action is required.

<jsp:setProperty name=”user” property=”*” />

Following code will be generated in the _jspService() method of the auto generated servlet:

//user object will be locked for thread safety
synchronized(user)
{
//Name property is set using the value name request parameter.
user.setName(request.getParameter("name"));
//MailId property is set using the value of mailId request parameter.
user.setMailId(request.getParameter("mailId"));
//Password property is set using the value of password request parameter.
user.setPassword(request.getParameter("password"));
}

Its time to recap the main concepts:

  1. setPropety action is used to set the values of external objects’ properties in JSP.
  2. It can set a property value directly.
  3. It can set a property value using a request parameter.
  4. All the properties values of an object can be set by a single action if the names of the request parameters and properties are same.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

One thought on “JSP setProperty action tag

Leave a Reply

Your email address will not be published. Required fields are marked *