Signup/Sign In

JSP jsp:setProperty Tag

The setProperty tag is used to store data in JavaBeans instances. The syntax of setProperty tag is:

<jsp:setProperty name="beanName" property="*">
<!-- or -->
<jsp:setProperty name="beanName" property="propertyName">
<!-- or -->
<jsp:setProperty name="beanName" property="propertyName" param="parameterName">
<!-- or -->
<jsp:setProperty name="beanName" property="propertyName" value="propertyValue">

The name attribute specifies the name of javaBean instances. This must match the id attribute specified in the jsp:useBean tag. The property attribute specifies which property of the bean to access.


Example of setProperty with Java Bean

Following is our Java class.

PersonBean.java


import java.io.Serializable;

public class PersonBean implements Serializable
{
  private String name;
  
  public PersonBean()
  {
    this.name="";
  }
  public void setName(String name)
  {
    this.name = name;
  }
  public String getName()
  {
    return name;
  }
}

hello.jsp

<html>
    <head>
        <title>Welcome Page</title>
    </head>
    <jsp:useBean id="person" class="PersonBean" scope="request" />
    <jsp:setProperty name="person" property="name" value="Viraj" />
    <body>
        Name of Person is : <jsp:getProperty name="person" property="name" />
    </body>
</html>

Output will be → Name of Person is : Viraj

Similarly we can have a very complex Java Bean as well, with many properties. We can easily get and set all the properties using the jsp:useBean, jsp:setProperty, jsp:getProperty.