JSP Standard Actions 'jsp:setProperty' & 'jsp:getProperty'


 

JSP Standard Actions 'jsp:setProperty' & 'jsp:getProperty'

In this section , we will discuss about Standard actions - 'jsp:setProperty' & 'jsp:getProperty' with an example.

In this section , we will discuss about Standard actions - 'jsp:setProperty' & 'jsp:getProperty' with an example.

JSP Standard Actions <jsp:setProperty> & <jsp:getProperty>

 <jsp:setProperty>

It sets a property value or values in a Bean only if a new object was instantiated, not if an existing one was found.

Syntax:

<jsp:setProperty
        name="beanInstanceName"
        {
            property= "*"   |
            property="propertyName" [ param="parameterName" ]   |
            property="propertyName" value="{string | <%= expression %>}"
        }
/>

Example :

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

<jsp:setProperty name="Ibean" property="user" />

<jsp:setProperty name="Ibean" property="user" value="Ankit" />

Attributes :

  • name="beanInstanceName"  :

The name of an instance of a Bean that has already been created or located with a <jsp:useBean> element. The value of name must match the value of id in <jsp:useBean>. The <jsp:useBean> element must appear before <jsp:seProperty> in the JSP file.

  • property="*"

The value of "*" means that all request parameters whose names match bean property names will be passed to the appropriate setter methods.

  • property="propertyName" [ param="parameterName" ]

Sets one Bean property to the value of one request parameter. In the syntax, property specifies the name of the Bean property and param specifies the name of the request parameter by which data is being sent from the client to the server.

If the Bean property and the request parameter have different names, you must specify both property and param. If they have the same name, you can specify property and omit param. If a parameter has an empty or null value, the corresponding Bean property is not set.

  • value="{string | <%= expression %>}

This optional attribute specifies the value for the property. String values are automatically converted to numbers, boolean, Boolean, byte, Byte, char, and Character via the standard valueOf method in the target or wrapper class. For example, a value of "true" for a boolean or Boolean property will be converted via Boolean.valueOf, and a value of "42" for an int or Integer property will be converted via Integer.valueOf.

<jsp:getProperty>

This element retrieves the value of a bean property, converts it to a string, and inserts it into the output. The two required attributes are:

  •  name: the name of a bean previously referenced via <jsp:useBean>.
  •  property: the property whose value should be fetched.

Syntax :

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

Limitations of <jsp:getProperty>:

  • You cannot use <jsp:getProperty> to retrieve the values of an indexed property.
  • You can use <jsp:getProperty> with JavaBeans components, but not with enterprise beans. As alternatives, you can write a JSP page that retrieves values from a Bean that in turn retrieves values from an enterprise bean, or you can write a custom tag that retrieves values from an enterprise bean directly.
Example : Given below a example that loads a bean and sets/gets a simple String parameter :
usebean.jsp

<HTML>

<head>

<TITLE>JavaBeans in JSP</TITLE>

<meta http-equiv="Content-Language" content="en-us">

<style type="text/css">

.style1 {

border: 2px solid #0000FF;

background-color: #CCFFFF;

}

</style>

</head>

<BODY>

<table class="style1" style="float: CENTER" align="center">

<tr>

<td style="width: 430px; height: 38px">

<h3><strong>EXAMPLE OF JSP:USEBEAN STANDARD ACTION</strong></h3>

0

</td>

</tr>

</table>

1

<P>

<jsp:useBean id="test" class="foo.usebeanexample" />

<jsp:setProperty name="test" property="message" value="Hello ANKIT" />

2

<center><H1>Message:

<jsp:getProperty name="test" property="message" />

</H1></center>

3

</BODY>

</HTML>

Given below is the code for the bean used in the "usebean.jsp" :

4

usebeanexample.java

package foo;

5

public class usebeanexample {

private String message = "No message specified";

6

public String getMessage() {

return(message);}

public void setMessage(String message) {

7

this.message = message;}

}


Output of  "usebean.jsp"




Download Source code

See Also :(click below)

JSP Standard Action <jsp:useBean>

Ads