JSP getProperty

JSP getProperty


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

In this tutorial you will learn about jsp:getProperty.

JSP getProperty

In this tutorial you will learn about jsp:getProperty.

In JSP <jsp:getProperty> tag is used to get the properties value of JavaBean component through getter method of that property. This element also displays the value, before displaying the value of property this tag converts the value of property to the String and then inserted to the response stream.

Attributes of <jsp:getProperty>

  • 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 ="propertyName" : A required name of property which value has to be displayed. Property is specified by the name declared as variable in the Bean and to get the value of this property a corresponding method must have in that Bean.

NOTE : To get the properties value of JavaBean components an Unified EL expression can also be used, using ${ } or using scriptlets <%=expression %>

How to use <jsp:getProperty>

<jsp:getProperty name="beanName" property="propName"/>

Example :

Here the example is given below demonstrating you about the use of <jsp:getProperty>. In this example I have created a Bean that contains some data members and their corresponding setter and getter methods. Then created a JSP page where designed a form to take input through user at run time then used the <jsp:setProperty> to set the properties value then created used the <jsp:getProperty> to get the value of the properties of corresponding set value.

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 getProperty</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 contained with form to fill the value will displayed to you

2. When you will fill the form and clicked on submit button then the output will be as follows :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics