logo

Set & Get Property


Show

Jsp:setProperty and Jsp:getProperty Action Tags

The setProperty and getProperty action tags are used for growing internet software with Java Bean. In web development, the bean class is generally used due to the fact it is a reusable software program element that represents data.

The jsp:setProperty action tag units a property value or values in a bean the usage of the setter method.

Syntax of jsp:setproperty Action Tag

<jsp:setProperty name="instanceOfBean" property= "*"   '   
property="propertyName" param="parameterName"  '   
property="propertyName" value="{ string ' <%= expression %>}"   
/>  

Example of Jsp:setProperty Action Tag if you have to Set all the values of Incoming Request in the Bean

<jsp:setProperty name="bean" property="*" /> 

Example of Jsp:setProperty Action Tag if you have to Set a Specific Value in the Property

<jsp:setProperty name="bean" property="username" value="Kumar" />  

Jsp:getProperty Action Tag

The jsp:getProperty action tag returns the value of the property.

Syntax of Jsp:getProperty Action Tag

<jsp:getProperty name="instanceOfBean" property="propertyName" /> 

Simple Example of Jsp:getProperty Action Tag

<jsp:getProperty name="obj" property="name" />

Example of Bean Development in JSP

There are three pages in the below given example:

  • index.html for the input of values
  • welocme.jsp file that sets the incoming values to the bean object and prints the one value
  • User.java bean class that have setter and getter methods

Index.html

<form  action="process.jsp" method="post">  
Name:<input type="text" name="name"><br>  
Password:<input type="password" name="password"><br>  
Email:<input type="text" name="email"><br>  
<input type="submit" value="register">  
</form> 

Process.jsp

<jsp:useBean id="u" class="org.sssit.User"></jsp:useBean>  
<jsp:setProperty property="*" name="u"/>    

Record:<br>  
<jsp:getProperty property="name" name="u"/><br>  
<jsp:getProperty property="password" name="u"/><br>  
<jsp:getProperty property="email" name="u" /><br> 

User.java

package org.sssit;  

public class User {  
private String name,password,email;  
//setters and getters  
}  

download this example



Reusing Bean in Multiple Jsp Pages

Let’s examine the simple example, that prints the data of bean components in two jsp pages.

Index.jsp

Same as given above

User.java

Same as given above

Process.jsp

<jsp:useBean id="u" class="org.sssit.User" scope="session"></jsp:useBean>  
<jsp:setProperty property="*" name="u"/>  
  
Record:<br>  
<jsp:getProperty property="name" name="u"/><br>  
<jsp:getProperty property="password" name="u"/><br>  
<jsp:getProperty property="email" name="u" /><br>  
  
<a href="second.jsp">Visit Page</a>

second.jsp

<jsp:useBean id="u" class="org.sssit.User" scope="session"></jsp:useBean>  
Record:<br>  
<jsp:getProperty property="name" name="u"/><br>  
<jsp:getProperty property="password" name="u"/><br>  
<jsp:getProperty property="email" name="u" /><br> 

Using Variable Value in SetProperty Tag

You might get some value from the database, in a few cases. That needs to be set in a bean object. In these types of cases, you have to use expression tags. For instance:

Process.jsp

<jsp:useBean id="u" class="org.sssit.User"></jsp:useBean>  
<%  
String name="arjun";  
%>  
<jsp:setProperty property="name" name="u" value="<%=name %>"/>  
  
Record:<br>  
<jsp:getProperty property="name" name="u"/><br>