Open In App

JSP – UseBean action tag

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

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:

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

<%@ 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:

Article Tags :