JSP setProperty

JSP setProperty


Posted in : Java Posted on : May 4, 2012 at 6:40 PM Comments : [ 0 ]

In this tutorial you will learn about .

JSP setProperty

In this tutorial you will learn about <jsp:setProperty>.

<jsp:setProperty> tag is used to set the value(s) of property(ies) in JavaBean component, to set the value(s) this tag uses the setter method defined in the Bean. This tag can't be used without <jsp:useBean> tag so, it is necessary to use the "useBean" tag to declare the Bean. You would have to careful while assigning the value of name attribute of <jsp:setProperty> tag, value of name must be the same as the value of id attribute of <jsp:useBean> tag.

Attributes of <jsp:setProperty>

  • name : Value of this attribute must be as same as the value of attribute id of <jsp:useBean> tag. So, <jsp:useBean> tag must be come along in the same JSP page.
  • property : This attribute can be used in various ways :
    • property="*" : This means in the matching Bean properties it will store all the values in the request parameters. Name of the Bean properties must be matched with the request parameters.
    • property="propertyName" [ param="parameterName"] : This means the value of single request parameter is set to a single Bean property. param can be used when the name of request parameter and the Bean property name is different and it can be omitted when the name of both request parameter and Bean property is same.
    • property="propertyName" value="{ String | <%= expression %> }" : This means a specific value will be set to a Bean property. Value of property can be a String or if a value is provided at runtime an expression can be used for it. The string value is converted to the data types of Bean property's data type whereas, in an expression the data type of the expression value must be the similar data type of the Bean property.

NOTE :

1. If value of parameter is null or empty then the corresponding Bean property will not be set. Similarly, if the Bean property doesn't have the same request parameter property value will not be set.

2. In <jsp:setProperty> tag param and value both can not be used simultaneously.

Using the <jsp:setProperty> tag value(s) can be set in the following ways :

  • String constant :
    <jsp:setProperty name="beanName" property="propName" value="string-constant"/>
  • request parameter :
    <jsp:setProperty name="beanName" property="propName" param="paramName"/>
  • expression :
    <jsp:setProperty name="beanName" property="propName" value="expression"/>
    
    <jsp:setProperty name="beanName" property="propName">
      <jsp:attribute name="value"> expression </jsp:attribute>
    </jsp:setProperty>
  • Name of the request parameter that matches the Bean property :
    <jsp:setProperty name="beanName" property="propName"/>
    <jsp:setProperty name="beanName" property="*"/>

Generally name of parameters are getting from the HTML form elements and the values are getting from the data the user inputs. These values are always a String type and sometimes these values are required to convert in other data types that's why these can be stored in Bean properties. To convert from String to other data type a permitted Bean property types and the corresponding conversion methods are as follows :

Property Type String Conversion Method
boolean or Boolean java.lang.Boolean.valueOf(String)
byte or Byte java.lang.Byte.valueOf(String)
char or Character java.lang.Character.valueOf(String)
double or Double java.lang.Double.valueOf(String)
integer or Integer java.lang.Integer.valueOf(String)
float or Float java.lang.Float.valueOf(String)
long or Long java.lang.Long.valueOf(String)

Example :

An example is given below will demonstrate you about how to set the values of property in JavaBean component. In this example I am trying to focused you only on setting the properties value in JavaBean component. For this at first I have created a Bean named Student which has some properties such as firstName, lastName, address. This Bean also contains their corresponding setter and getter methods these methods will help in to set and get the corresponding value of JavaBean component. Now we have to set the properties value using the jsp:setProperty tag so created a JSP page where I have designed a form where the First name, last name, and address can be given at run time or to take input through user. In the next steps I have checked for the null value and in the next step I have used the jsp:useBean to instantiate the Student Bean by the id="stu" because before setting the property(ies) value(s) of a JavaBean a Bean must be located i.e which Bean property value has to be set must be known and also because of a jsp:setPropery attribute name is required to identify the Bean and this name attribute identifies the Bean using the attribute id of jsp:useBean.

Student.java

package devmanuals;

public class Student {
	String firstName;
	String lastName;
	String address;
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}

}

jspSetProperty.jsp

<%@page import="org.omg.Dynamic.Parameter"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP setProperty</title>
</head>
<body>
<form>
<table>
<tr><td>Enter First Name :</td>
<td><input type="text" name="fName"/></td>
</tr>
<tr>
<td>Enter Last Name :</td>
<td><input type="text" name="lName"/></td>
</tr>
<tr>
<td>Enter Address :</td>
<td><input type="text" name="add"/></td>
</tr>
<tr>
<td></td><td><input type="submit" value="submit" /></td>
</tr>
</table>
</form>
<%
String firstName = request.getParameter("fName");
String address = request.getParameter("add");
if(firstName != null)
{
%>
<jsp:useBean id="stu" class="devmanuals.Student" scope="request"/>

<jsp:setProperty property="firstName" value="<%=firstName %>" name="stu"/>
<jsp:setProperty property="lastName" value="${param.lName}" name="stu"/>
<jsp:setProperty property="address" value="<%=address %>" name="stu"/>
<h3>Student details are :</h3>
<table>
<tr>
<td>First Name : </td>
<td><jsp:getProperty property="firstName" name="stu"/></td>
</tr>
<tr>
<td>Last Name : </td>
<td><jsp:getProperty property="lastName" name="stu"/></td>
</tr>
<tr>
<td>Address : </td>
<td><jsp:getProperty property="address" name="stu"/></td>
</tr>
</table>
<%
}
%>
</body>
</html>

Output :

When you will execute the above JSP page you will get the output as follows :

1. A page will be displayed to you for inputting the value

2. When you will input the value in their corresponding text box you will get the output as follows :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics