JSP Actions

This article is a series of JSP Tutorial. In this article, we will learn what are JSP Actions and how to use JSP actions with an example.
Before getting started with JSP Actions, let's quickly familiar with few terms like JavaBean, Bean properties, JavaBean example etc.

What Is a JavaBean?

A bean is simply a Java class that meets these requirements:
  • It has a zero-argument constructor.
  • It implements Serializable or Externalizable to make it persistent.
  • It may have a number of properties which can be read or written.
  • It may have a number of "getter" and "setter" methods for the properties.

JavaBeans Properties

A JavaBean property is a named attribute that can be accessed by the user of the object. The attribute can be of any Java data type, including the classes that you define.
A JavaBean property may be read, write, read-only, or write-only. JavaBean properties are accessed through two methods in the JavaBean's implementation class −
  1. getPropertyName() - For example, if property name is firstName, your method name would be getFirstName() to read that property. This method is called accessor.
  2. setPropertyName() - For example, if property name is firstName, your method name would be setFirstName() to write that property. This method is called mutator.
A read-only attribute will have only a getPropertyName() method, and a write-only attribute will have only a setPropertyName() method.

Simple JavaBeans Example

Consider a Student class with few properties −
public class StudentsBean implements java.io.Serializable {
   private String firstName ;
   private String lastName;
   private int age;

   public StudentsBean() {
   }
   public String getFirstName(){
      return firstName;
   }
   public String getLastName(){
      return lastName;
   }
   public int getAge(){
      return age;
   }
   public void setFirstName(String firstName){
      this.firstName = firstName;
   }
   public void setLastName(String lastName){
      this.lastName = lastName;
   }
   public void setAge(Integer age){
      this.age = age;
   }
}

JSP Actions

As you’ve seen, JavaBeans are also Java classes and, as such, can be created and manipulated in JSP pages using scriptlets, declarations, and expressions.
The JSP specification provides special support for beans that make them scriptable at a higher level, however. This support consists of three standard actions:
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.

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 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 firstname, lastName, emailId 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:

In the next article, we will learn how to process HTML forms using JSP.

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments