SetProperty And GetProperty In JSP

Introduction

In this article we discuss setProperty and getProperty in JSP action tags.

The setProperty and getProperty action tags are used for developing web applications with Java Beans. In web development, The 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

<jsp:setProperty name="instanseofBean" property="*" | property="propertyName" param="parameterName" | property="propertyName" value="{String | }"/>

Example

jsp:setProperty action tag is for when we need to set all the values of an incoming request in the Bean; see:

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

Example

We use the jsp:setProperty action tag if we need to set the value of an incoming-specific property; see:

<jsp:setProperty name="beanname" property="username"/>

Example

We use the jsp:setProperty action tag if we need to set a specifiv=c value in the property; see:

<jsp:setProperty name="beanname" property="username" values="sharma"/>

jsp:getProperty action tag

This action tag returns the value of the property.

Syntax

<jsp:getProperty name="instanceOfBean" property="nameofproperty"/>

Simple Example

<jsp:getProperty name="objname" property="nameofproperty"/>

Example of Bean development in JSP:

In this example there are 3 pages:

  • home.html
  • user.jsp
  • Username.java

home.html

<!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>Insert title here</title>

</head>

<body>

<form action="user.jsp"/>

Enter your Name:<input type="text" name="name"/>

</form>

</body>

</html>

user.jsp

<%@ 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>Insert title here</title>

</head>

<body>

<jsp:useBean id="ob" class="mypack.Username"/>

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

Welcome- <jsp:getProperty name="ob" property="name"/>

</body>

</html>

Username.java

package mypack; 

public class Username

  {

    private String name;

    public void setName(String name)

      {

        this.name=name;

      }

    public String getName()

      {

        return name;

      }

  }

Output

fig-1.jpg

When we enter some name (like Sandy) then the following output will be shown:

Fig-2.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all