JSP useBean, setProperty and getProperty Example



The JSP specification provides special support for beans that make them scriptable at a higher level, however. This support consists of three standard actions:
  • jsp:useBean for declaring, instantiating, and initializing beans
  • jsp:setProperty for setting bean properties
  • jsp:getProperty for retrieving bean property values
Let's quickly discuss each of these actions briefly and later we will look into a complete Student Registration Example using these JSP action tags.
All JSP Examples at JSP Source Code Examples

jsp:useBean

The jsp:useBean action tag is used to locate or instantiate a bean class. If bean object of the Bean class is already created, it doesn't create the bean depending on the scope. But if an object of the bean is not created, it instantiates the bean.
Syntax of jsp:useBean action tag:
<jsp:useBean id= "instanceName" scope= "page | request | session | application"   
   class= "packageName.className" type= "packageName.className"  
   beanName="packageName.className | <%= expression >" >  
</jsp:useBean>  

jsp:setProperty

The jsp:setProperty action assigns values to bean properties based on values in the JSP page.
Syntax of jsp:setProperty action tag:
<jsp:setProperty name="instanceOfBean" property= "*"   |   
   property="propertyName" param="parameterName"  |   
   property="propertyName" value="{ string | <%= expression %>}"   
/>  

jsp:getProperty

Bean property values can be retrieved with the jsp:getProperty action.
Syntax of jsp:getProperty action tag:
<jsp:getProperty name=”name” property=”property”/>
where name is the bean with the corresponding id attribute, and the property is the name of the property desired.

A complete example of useBean, setProperty and getProperty

  1. Let's create a bean class Student which is having four variables firstnamelastNameemailId and password. In order to use the bean class and it’s properties in JSP we have initialized the class like this in the studentdetails.jsp page –
<jsp:useBean id="student" class="net.javaguides.jsp.tutorial.Student"></jsp:useBean>
We have used useBean action to initialize the class. Our class is in net.javaguides.jsp.tutorial package so we have given a fully qualified name net.javaguides.jsp.tutorial.Student.
  1. We have mapped the properties of bean class and JSP using setProperty action tag. We have given ‘*’ in the property field to map the values based on their names because we have used the same property name in bean class and index.jsp JSP page. In the name field we have given the unique identifier which we have defined in a useBean tag.
<jsp:setProperty property="*" name="student" />
  1. To get the property values we have used getProperty action tag.
<b>First Name:</b> <jsp:getProperty property="firstName" name="student" /><br><br>
<b>Last Name:</b> <jsp:getProperty property="lastName" name="student" /><br><br>
<b>Email ID:</b><jsp:getProperty property="emailId" name="student" /><br><br>
<b>Password:</b> <jsp:getProperty property="password" name="student" />

Student.java

package net.javaguides.jsp.tutorial;

import java.io.Serializable;

public class Student implements Serializable {

    private static final long serialVersionUID = 1 L;

    private String firstName;
    private String lastName;
    private String emailId;
    private String password;
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getEmailId() {
        return emailId;
    }
    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

student.jsp

Let's create a student registration form:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>JSP Actions Example</title>
</head>
<body>

     <h1> Student Registration Page</h1>
     <form action="studentdetails.jsp" method="post">
         First Name: <input type="text" name="firstName">
         <br> <br> 
  
         Last Name: <input type="text" name="lastName">
        <br> <br> 
  
         Email ID: <input type="email" name="emailId">
         <br> <br> 
  
         Password: <input type="password" name="password"><br>
  
         <br> 
         <input type="submit" value="register">
       </form>
</body>
</html>

studentdetails.jsp

Let's create studentdetails.jsp page to show student registered details:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

    <jsp:useBean id="student" class="net.javaguides.jsp.tutorial.Student"></jsp:useBean>
    <jsp:setProperty property="*" name="student" />
    <h1>Student Registered with Following Details</h1>
    <b>First Name:</b> <jsp:getProperty property="firstName" name="student" /><br><br>
    <b>Last Name:</b> <jsp:getProperty property="lastName" name="student" /><br><br>
    <b>Email ID:</b><jsp:getProperty property="emailId" name="student" /><br><br>
    <b>Password:</b> <jsp:getProperty property="password" name="student" />
</body>
</html>

Output

Student Registration Page

Fill details and submit a student registration form:
All JSP Examples at JSP Source Code Examples.

Comments