JavaBeans are reusable software components written in Java that follow specific conventions. In enterprise applications, JavaBeans are commonly used for data encapsulation and as building blocks in frameworks like Java EE. This article explains how to create JavaBeans and use them effectively in enterprise applications.
A JavaBean is a simple Java class that adheres to the following conventions:
java.io.Serializable
interface.Example of a JavaBean:
import java.io.Serializable; public class Employee implements Serializable { private int id; private String name; private double salary; // No-argument constructor public Employee() {} // Getters and Setters public int getId() { return id; } public void setId(int id) { this.id = id; } 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; } }
JavaBeans are often used in JSP applications to manage data between the presentation layer and the business logic layer.
Example: Using the Employee
JavaBean in a JSP page:
<%-- Import the Employee bean in a JSP --%> <jsp:useBean id="employee" class="Employee" scope="session" /> <%-- Set properties --%> <jsp:setProperty name="employee" property="id" value="101" /> <jsp:setProperty name="employee" property="name" value="John Doe" /> <jsp:setProperty name="employee" property="salary" value="50000" /> <%-- Get properties --%> <h2>Employee Details</h2> <p>ID: <jsp:getProperty name="employee" property="id" /></p> <p>Name: <jsp:getProperty name="employee" property="name" /></p> <p>Salary: <jsp:getProperty name="employee" property="salary" /></p>
JavaBeans can be used in servlets to encapsulate data and share it with the client-side code.
Example: Passing an Employee
bean to a JSP from a servlet:
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class EmployeeServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Employee employee = new Employee(); employee.setId(102); employee.setName("Jane Doe"); employee.setSalary(60000); // Set the bean as a request attribute request.setAttribute("employee", employee); // Forward to a JSP RequestDispatcher dispatcher = request.getRequestDispatcher("employeeDetails.jsp"); dispatcher.forward(request, response); } }
JSP to display the Employee
bean:
<%-- Access the Employee bean from the request scope --%> <% Employee employee = (Employee) request.getAttribute("employee"); %> <h2>Employee Details</h2> <p>ID: <%= employee.getId() %></p> <p>Name: <%= employee.getName() %></p> <p>Salary: <%= employee.getSalary() %></p>
In enterprise applications, JavaBeans are often used to interact with databases and transfer data between layers. This is achieved using frameworks like Java EE or Spring.
Example: Using JavaBeans with JDBC:
import java.sql.*; public class EmployeeDAO { public Employee getEmployeeById(int id) throws SQLException { Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/employees", "root", "password"); PreparedStatement stmt = conn.prepareStatement("SELECT * FROM employee WHERE id = ?"); stmt.setInt(1, id); ResultSet rs = stmt.executeQuery(); Employee employee = new Employee(); if (rs.next()) { employee.setId(rs.getInt("id")); employee.setName(rs.getString("name")); employee.setSalary(rs.getDouble("salary")); } conn.close(); return employee; } }
Example servlet using the DAO:
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class EmployeeDBServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { EmployeeDAO dao = new EmployeeDAO(); Employee employee = dao.getEmployeeById(101); request.setAttribute("employee", employee); RequestDispatcher dispatcher = request.getRequestDispatcher("employeeDetails.jsp"); dispatcher.forward(request, response); } catch (Exception e) { e.printStackTrace(); } } }
JavaBeans are a vital part of enterprise applications, providing a standardized way to encapsulate and manage data. By combining JavaBeans with JSP, servlets, and frameworks, developers can build scalable, maintainable, and efficient web applications.