Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

set and getProperty


The setProperty and getProperty action tags are used for developing web application with Java Bean. In web devlopment, bean class is mostly used because it is a reusable software component that represents data.

The jsp:setProperty action tag sets a property value or values in a bean using the setter method.

Syntax of jsp:setProperty action tag

  1. <jsp:setProperty name=“instanceOfBean” property= “*”   |
  2. property=“propertyName” param=“parameterName”  |
  3. property=“propertyName” value=“{ string | <%= expression %>}”
  4. />

Example of jsp:setProperty action tag if you have to set all the values of incoming request in the bean

  1. <jsp:setProperty name=“bean” property=“*” />

Example of jsp:setProperty action tag if you have to set value of the incoming specific property

  1. <jsp:setProperty name=“bean” property=“username” />

Example of jsp:setProperty action tag if you have to set a specific value in the property

  1. <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

  1. <jsp:getProperty name=“instanceOfBean” property=“propertyName” />

Simple example of jsp:getProperty action tag

  1. <jsp:getProperty name=“obj” property=“name” />

Example of bean development in JSP

In this example there are 3 pages:

  • index.html for 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

  1. <form action=“process.jsp” method=“post”>
  2. Name:<input type=“text” name=“name”><br>
  3. Password:<input type=“password” name=“password”><br>
  4. Email:<input type=“text” name=“email”><br>
  5. <input type=“submit” value=“register”>
  6. </form>

process.jsp

  1. <jsp:useBean id=“u” class=“org.sssit.User”></jsp:useBean>
  2. <jsp:setProperty property=“*” name=“u”/>
  3. Record:<br>
  4. <jsp:getProperty property=“name” name=“u”/><br>
  5. <jsp:getProperty property=“password” name=“u”/><br>
  6. <jsp:getProperty property=“email” name=“u” /><br>

User.java

  1. package org.sssit;
  2. public class User {
  3. private String name,password,email;
  4. //setters and getters
  5. }

setproperty1

setproperty2

Reusing Bean in Multiple Jsp Pages

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

index.jsp

Same as above.


User.java

Same as above.


process.jsp

  1. <jsp:useBean id=“u” class=“org.sssit.User” scope=“session”></jsp:useBean>
  2. <jsp:setProperty property=“*” name=“u”/>
  3. Record:<br>
  4. <jsp:getProperty property=“name” name=“u”/><br>
  5. <jsp:getProperty property=“password” name=“u”/><br>
  6. <jsp:getProperty property=“email” name=“u” /><br>
  7. <a href=“second.jsp”>Visit Page</a>

second.jsp

  1. <jsp:useBean id=“u” class=“org.sssit.User” scope=“session”></jsp:useBean>
  2. Record:<br>
  3. <jsp:getProperty property=“name” name=“u”/><br>
  4. <jsp:getProperty property=“password” name=“u”/><br>
  5. <jsp:getProperty property=“email” name=“u” /><br>

Using variable value in setProperty tag

In some case, you may get some value from the database, that is to be set in the bean object, in such case, you need to use expression tag. For example:

process.jsp

  1. <jsp:useBean id=“u” class=“org.sssit.User”></jsp:useBean>
  2. <%
  3. String name=“arjun”;
  4. %>
  5. <jsp:setProperty property=“name” name=“u” value=“<%=name %>”/>
  6. Record:<br>
  7. <jsp:getProperty property=“name” name=“u”/><br>

Comments

comments