Enterprise Java Bean
Enterprise Java Bean
A JavaBean is a specially constructed Java class written in the Java and coded according to the JavaBeans
API specifications.
Following are the unique characteristics that distinguish a JavaBean from other Java classes −
•It provides a default, no-argument constructor.
•It should be serializable and that which can implement the Serializable interface.
•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.
JavaBean 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 −
JavaBean properties are the attributes or fields of a JavaBean class. These properties follow
naming conventions:
•getPropertyName() – for reading the property/ This method is called accessor.
•setPropertyName(value) – for writing the property / This method is called mutator.
Types of Properties:
Here values for the scope attribute can be a page, request, session or application based on
your requirement. The value of the id attribute may be any value as a long as it is a unique
name among other useBean declarations in the same JSP.
Accessing JavaBeans Properties
Along with <jsp:useBean...> action, you can use the <jsp:getProperty/> action to access the get methods and
the <jsp:setProperty/> action to access the set methods. Here is the full syntax −
The name attribute references the id of a JavaBean previously introduced to the JSP by the useBean action.
The property attribute is the name of the get or the set methods that should be invoked.
Types of Enterprise JavaBeans
Session Beans:
•Stateful:
Maintain state across multiple method calls for a specific client, managing business functions for a client's
session.
•Stateless:
Do not maintain state across calls and are used for business services, providing stateless logic.
•Singleton:
A single instance of the bean is shared by all clients, providing a shared resource.
Entity Beans:
•Deprecated in newer versions of Java EE, with JPA (Java Persistence API) being the
preferred approach for persistence.
Message-driven Beans:
Key Features:
•No data is retained between method calls.
•Lightweight and scalable.
•Annotated with @Stateless.
Annotations used in Stateless Session Bean
There are 3 important annotations used in stateless session bean:
1.@Stateless
2.@PostConstruct
3.@PreDestroy
Key Features:
•Holds data specific to a client.
•Destroyed when the client session ends.
•Annotated with @Stateful.
Conversational state between multiple method calls is maintained by the container in stateful session bean.
Annotations used in Stateful Session Bean
There are 5 important annotations used in stateful session bean:
1.@Stateful
2.@PostConstruct
3.@PreDestroy
4.@PrePassivate
5.@PostActivate
A message driven bean is like stateless session bean that encapsulates the business logic and doesn't maintain state.
Message Driven Bean Example
To create the message driven bean, you need to declare @MessageDriven annotation and implement MessageListener
interface.
In eclipse ide, create EJB Project then create a class as given below:
In glassfish server, click on applications -> deploy -> select mdb jar file
by Choose File -> OK.
B. Entity Beans (Deprecated)
•Used to represent data stored in a database.
•Mapped to a row in a table.
•Replaced by JPA (Java Persistence API) from EJB 3.x onwards.
Key Features:
•Persistent by nature.
•Automatically synchronized with the database.
•Annotated with @Entity.
EJB Container
The EJB Container is a runtime environment provided by the application server (such as WildFly, GlassFish,
or JBoss) that manages the execution of Enterprise JavaBeans (EJBs). It provides essential services to the
beans automatically so that the developer can focus on business logic rather than system-level concerns.
Annotations Used:
•@PostConstruct: Called after bean creation.
•@PreDestroy: Called before bean destruction.
Stateful Session Bean Lifecycle
Annotations Used:
•@PostConstruct, @PreDestroy, @PrePassivate, @PostActivate