Open In App

JSP – UseBean action tag

Last Updated : 02 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Java, JavaServer Pages provides the mechanism to interact with the Java objects called JavaBeans. The useBean action tag is used to instantiate and access JavaBeans within the JSP pages. This allows the developers to encapsulate the data and business logic in the Java classes and it can easily integrate them into the JSP pages.

useBean action tag in JSP

The <jsp: useBean> event tag in JSP can be used to create or retrieve a JavaBean instance and provides a way to use Java objects in JSP pages without the need for explicit Java code This tag is consumed Automatic handling of JavaBeans modeling and management, so that developers Java in JSP applications It is easier to work with objects.

Syntax:

<jsp:useBean id="beanName" class="fullyQualifiedClassName" scope="scope"/>

Step-by-Step Implementation of useBean action tag in JSP

  • Create the JavaBean Class: First, we develop the Java class that can encapsulate the data, and it provides the getter and setter methods of the JSP application.
  • Deploy the JavaBean Class: Then we will compile the JavaBean class and deploy it to the web applications classpath of the JSP application.
  • Use the <jsp: useBean > tag: Here, we will utilize the <jsp: useBean > action tag within the JSP pages to instantiate or access JavaBeans.
  • Access the JavaBean Properties: Use the expressions or the JSP scriptlets to access the properties of the JavaBean within the JSP pages.

Project to Implement JSP useBean action tag

Below we have created index.jsp and EmployeeBean.java and tested the output in the browser.

Note: Refer this link to know the create and execute he jsp projects.

EmployeeBean.java:

Java
package example;
public class EmployeeBean {
    private String name;
    private double salary;

    // Getter and setter methods
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

}

index.jsp

HTML
<%@ page language="java"  contentType="text/html;  charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="example.EmployeeBean" %>
<!DOCTYPE html>

<html>
<head>
    <meta charset="UTF-8">
    <title>UseBean Example</title>
</head>

<body>
    <h2>UseBean Example</h2>
    <jsp:useBean id="employee" class="example.EmployeeBean" />
    <jsp:setProperty name="employee" property="name" value="Mahesh Kadambala"/>
    <jsp:setProperty name="employee" property="salary" value="50000"/>
    
    <p>Name: <%= employee.getName() %></p>
    <p>Salary: <%= employee.getSalary() %></p>
</body>
</html>

Output:

Below we can see the output in browser.

Output Screen

Explanation of the Example:

  • In the above example, we have created the instance of the EmployeeBean JavaBean using <jsp: useBean> tag with ID employee and set the scope to the session.
  • Then we set the name and salary properties of the EmployeeBean using <jsp:setProperty> tag.
  • Finally, it retrieves and display the name and salary properties of the EmployeeBean as output using the Java expression tags.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads