The setProperty and getProperty action tags are used for growing internet software with Java Bean. In web development, the bean class is generally used due to the fact it is a reusable software program element that represents data.
The jsp:setProperty action tag units a property value or values in a bean the usage of the setter method.
<jsp:setProperty name="instanceOfBean" property= "*" ' property="propertyName" param="parameterName" ' property="propertyName" value="{ string ' <%= expression %>}" />
<jsp:setProperty name="bean" property="*" />
<jsp:setProperty name="bean" property="username" value="Kumar" />
The jsp:getProperty action tag returns the value of the property.
<jsp:getProperty name="instanceOfBean" property="propertyName" />
<jsp:getProperty name="obj" property="name" />
There are three pages in the below given example:
Index.html
<form action="process.jsp" method="post"> Name:<input type="text" name="name"><br> Password:<input type="password" name="password"><br> Email:<input type="text" name="email"><br> <input type="submit" value="register"> </form>
Process.jsp
<jsp:useBean id="u" class="org.sssit.User"></jsp:useBean> <jsp:setProperty property="*" name="u"/> Record:<br> <jsp:getProperty property="name" name="u"/><br> <jsp:getProperty property="password" name="u"/><br> <jsp:getProperty property="email" name="u" /><br>
User.java
package org.sssit; public class User { private String name,password,email; //setters and getters }
Let’s examine the simple example, that prints the data of bean components in two jsp pages.
Index.jsp
Same as given above
User.java
Same as given above
Process.jsp
<jsp:useBean id="u" class="org.sssit.User" scope="session"></jsp:useBean> <jsp:setProperty property="*" name="u"/> Record:<br> <jsp:getProperty property="name" name="u"/><br> <jsp:getProperty property="password" name="u"/><br> <jsp:getProperty property="email" name="u" /><br> <a href="second.jsp">Visit Page</a>
second.jsp
<jsp:useBean id="u" class="org.sssit.User" scope="session"></jsp:useBean> Record:<br> <jsp:getProperty property="name" name="u"/><br> <jsp:getProperty property="password" name="u"/><br> <jsp:getProperty property="email" name="u" /><br>
You might get some value from the database, in a few cases. That needs to be set in a bean object. In these types of cases, you have to use expression tags. For instance:
Process.jsp
<jsp:useBean id="u" class="org.sssit.User"></jsp:useBean> <% String name="arjun"; %> <jsp:setProperty property="name" name="u" value="<%=name %>"/> Record:<br> <jsp:getProperty property="name" name="u"/><br>