09 - JSP Actions: Page 3 of 3

9.8 <jsp:setProperty>

The action tag <jsp:setProperty> is used to set the value of attribute of a java bean instance. <jsp:setProperty> tag can set the value directly from the request parameter or can set explicit value or even can set all the request parameters with all bean attributes (provided bean attributes and request parameter names matches)

The syntax of <jsp:setProperty> tag are –

     · <jsp:setProperty name=”bean_name” property=”property_name” value=”explicit_value” />

This form of <jsp:setProperty> will populate property provided in ‘property’ attribute of bean given in ‘name’ attribute with the value given in ‘value’ attribute.

Example –

<jsp:setProperty name=”person” property=”firstName” value=”Joe” /> is equivalent to
person.setFirstName(“Joe”) ;
· <jsp:setProperty name=”bean_name” property=”property_name” param=”request param” />

This form of <jsp:setProperty> will populate property provided in ‘property’ attribute of bean given in ‘name’ attribute with the value of the request parameter given in ‘value’ attribute.

Example –

<jsp:setProperty name=”person” property=”firstName” param=”name” /> is equivalent to
person.setFirstName(request.getParameter(“name”)) ;
· <jsp:setProperty name=”bean_name” property=”property_name” />

This form of <jsp:setProperty> will populate property provided in ‘property’ attribute of bean given in ‘name’ attribute with the value of the request parameter having same name as attribute name.

Example –

<jsp:setProperty name=”person” property=”firstName” /> is equivalent to
person.setFirstName(request.getParameter(“firstName”)) ;
· <jsp:setProperty name=”bean_name” property=”*” />

This form of <jsp:setProperty> will populate all the properties of bean given in ‘name’ attribute with the values of the request parameters having same name as attribute names.

Example –

<jsp:setProperty name=”person” property=”*” /> is equivalent to
person.setFirstName(request.getParameter(“firstName”)) ;
person.setLastName(request.getParameter(“lastName”)) ;
(assuming there are two fields in Person class)

To see above tags in action , lets update the useBean.jsp tag to use <set:Property> tag

<html>
  <head>
    <title> Use Bean Example </title>
  </head>
  <body>
    <jsp:useBean id="person" class="com.sample.jsp.tutorial.Person" scope="request">
    Hello<br/>
    </jsp:useBean>
    First Name :: <jsp:getProperty property="firstName" name="person"/>
    <br/>
    Last Name :: <jsp:getProperty property="lastName" name="person"/>
    <jsp:setProperty name="person" property="firstName" value="First Name" />
    <jsp:setProperty name="person" property="lastName" value="Last Name" />
    <br/>
    <jsp:include page="anotherUseBean.jsp"></jsp:include>
  </body>
</html>

To see another forms of this tag in action, lets implement this scenario-

a) Create a java class PersonalDetails with two field (name and email address)

b) create one inputDetails.jsp having name and email address as a input field in a form.

c) Form will be submitted to displayDetails.jsp. displayDetails.jsp will display the values entered by user in inputDetails.jsp.

d) displayDetails.jsp will set the input in PersonDetails bean.

a) PersonDetail.java

package com.sample.jsp.tutorial;
public class PersonDetails {
   private String name;
   private String emailAddress;
   public String getName()
   {
     return name;
   }
   public void setName(String name)
   {
     this.name = name;
   }
   public String getEmailAddress()
   {
     return emailAddress;
   }
   public void setEmailAddress(String emailAddress)
   {
     this.emailAddress = emailAddress;
   }
}

B) inputDetails.jsp

<html>
  <head>
    <title>Input Form</title>
  </head>
  <body>
    <form name="form" action="displayDetails.jsp"
      method="POST">
      Name: <input type="text" name="name" /> <br />
      Email:<input type="text" name="emailAddress" />
      <br/>
      <input type="submit" value="Submit" />
    </form>
  </body>
</html>

B) displayDetails.jsp

<html>
  <head>
    <title>Display Details</title>
  </head>
  <body>
    <jsp:useBean id="personDetails" scope="request" class="com.sample.jsp.tutorial.PersonDetails" />
    <jsp:setProperty name="personDetails" property="name" param="name"/>
    <jsp:setProperty name="personDetails" property="emailAddress" />
    Name :: <jsp:getProperty property="name" name="personDetails"/>
    <br/>
     Email :: <jsp:getProperty property="emailAddress" name="personDetails"/>
    <br/>
  </body>
</html>

C) Testing – Access inputDetails.jsp by accessing http://localhost:8080/jsp-tutorial/inputDetails.jsp . Enter details and click submit

We can see the entered data. In the displayDetails.jsp we have used two forms of <jsp:setProperty> tags. In case of email address we have skipped param attribute because input field and bean property has same name.

This is the same case with name field as well but to show I have used param tag

As the both property and request parameter has a same name we can change the displayDetails.jsp to use * as highlighted below

<html>
  <head>
    <title>Display Details</title>
  </head>
  <body>
    <jsp:useBean id="personDetails" scope="request" class="com.sample.jsp.tutorial.PersonDetails" />
    <jsp:setProperty name="personDetails" property="*" />
    Name :: <jsp:getProperty property="name" name="personDetails"/>
    <br/>
       Email :: <jsp:getProperty property="emailAddress" name="personDetails"/>
    <br/>
  </body>
</html>

9.9 <jsp:element> , <jsp:attribute> , <jsp:body>

These JSP tags are used to create xml elements dynamically because attributes or elements or values can be created dynamically.

For example below tags are taking dynamic values from request object

<html>
  <head>
    <title>XML Page</title>
  </head>
  <body>
    <jsp:element name="ClientAddress">
    <jsp:attribute name="port"><%= request.getLocalPort() %></jsp:attribute>
    <jsp:body>Host Name is <%= request.getLocalName()%></jsp:body>
    </jsp:element>
  </body>
</html>

Will generate below xml tag. You can see xml element, attribute value and body text all are generated dynamically

<ClientAddress port="8080">
Host Name is 127.0.0.1
</ClientAddress>

You can verify the above generated tag by view source (refer below figure )

 

Like us on Facebook