Académique Documents
Professionnel Documents
Culture Documents
Sun Microsystems introduced the J2EE application server and the Enterprise
JavaBean (EJB) specifications as a venture into the multitier server side component
architecture market. It is important to note that though EJB and JavaBeans are both
component models, they are not same. EJBs are interprocess components and JavaBeans
are intraprocess components. EJB is a specification for creating server side components
that enables and simplifies the task of creating distributed objects.
Client
Home Interface
EJB Home Home Interface
EJB
Home
Stub EJB Server
Remote Interface EJB Object
EJB Container Remote Intf
EJB
Object
Stub Bean Class
Enterprise bean, which contains methods that implement the business logic.
EJB Server
EJB Server provides some low level services, such as network connectivity to the
container. It also provides the following services:
4. Precached Instances – The EJB server maintains a cache. This cache information
about the state of the enterpirse bean.
EJB Container
The EJB container acts as an interface between and Enterprise bean and the clients.
Clients communicate with the enterprise bean through the Remote and Home Interfaces
provide by the container.
The client communicating to the enterprise bean through the container enables the
container to service the client’s request with great flexibility. For Instance, the container
manages a pool of enterprise beans and uses them as the need arises, instead of creating a
new instance of the bean for each client request.
1. Security
2. Transaction Management
3. Persistance -> Permanent Storage
4. Life Cycle Management
Enterprise Bean
Enterprise JavaBeans are write once, run anywhere, middle tier components that consists
of methods that implements the business rule. The enterprise bean encapsulates the
business logic. There are two types of enterprise bean.
1. Entity Bean
2. Session Bean
Entity Bean
Entity Beans are enterprise beans that persist across multiple sessions and
multiple clients. There are two types of Entity bean:
In a Bean managed persistance, the programmer has to write the code for database
calls. On the other hand, in container managed persistance, the container takes care of
database calls.
Session Bean
newInstance()
setSessionContext() ejbRemove()
ejbCreate()
Method Ready
Does not Exists -> when the bean has not been instantiated.
Method Ready -> When the container requires it.
Class.newInstance() -> Create a new instance of the stateless session bean and allocates
the required memory
ejbCreate() -> It is similar to the constructor of EJB class. It is invoked only once in the
life cycle of the stateless session bean, when the client invokes the create() method of the
home interface. The ejbCreate() method must not take any argument, as stateless session
bean do not store any information in the instance variable.
ejbRemove() -> ends the life cycle of the stateless session bean. This method closes any
open resource and frees the memory space.
Stateless Bean
Home Interface
Remote Interface
Bean Class
Client Application
Remote Interface
A remote interface defines all the business methods of the enterprise bean that the
EJB client would invoke. The remove interface does not include system level operations,
such as persistence, security and transactions.
Home Interface
The home interface defines methods that allow EJB clients to create and find EJB
components.
Bean Class
The EJB class implements all the business methods declared in the remote
interface.
Eg:
Calculator.java
//Remote Interface
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
CalculatorHome.java
// Home Interface
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
CalculatorEJB.java
public CalculatorEJB() {}
public void ejbCreate() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext sc) {}
}
s.bat
path=%path%;d:\jdk1.2\bin;d:\j2sdkee1.2\bin
set classpath=d:\j2sdkee1.2\lib\j2ee.jar;d:\demo\ejb;
set java_home=d:\jdk1.2
set j2ee_home=d:\j2sdkee1.2
EJB Servers
Sun Java Server (J2EE Server)
Web Logic (BEA)
Websphere (IBM)
J2EE SDK Architecture
• J2EE Server
• EJB Container
• Web Container
Enterprise Bean
EJB Container
Enterprise Bean
J2EE Server
JSP File
Web Container
Servlet
J2EE Server
The J2EE server contains the EJB container and the Web Container. The J2EE
server provides the following services to the EJB container and the Web container.
EJB Container
The EJB Container contains the enterprise bean. The container provides remote
and home interfaces through which clients communicate with the enterprise bean. It also
provides services such as transaction management, security, life cycle management and
persistence.
Web Container
The Web container is a runtime environment for servlets and JSP files.
A J2EE application client is a Java application that runs in an environment, which enable
it to access the J2EE services. A J2EE application client is packaged in to .jar file
A Web component may contain servlet, JSP, HTML and GIF files. These Web
component files are packaged in a .war file.
An enterprise bean consists of three files: the EJB class, home and remote interfaces. The
enterprise bean files are bundled into an EJB. Jar file.
The .jar,.war and EJB.jar files are assembled into a J2EE application, which is an .ear
file. The .ear file is then deployed on the J2EE server.
Assembled
J2EE application
(.ear file)
Deployed
J2EE
server
Deployment Descriptor
An enterprise beans has to be managed at runtime. Moreover, the J2EE server
must know how to apply the primary services, such as security, naming, and transaction
service. To do this, deployment descriptor is used.
The client uses JNDI to initiate a connection to and EJB server and to locate a specific
EJB home.
CalculatorClient.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
public CalculatorClient()
{
super(str);
c = getContentPane();
c.setLayout(new GridLayout(2,2,2,2));
t = new JTextField(10);
c.add(t);
b = new JButton("Calculator");
c.add(b);
compile CalculatorClient.java
Before Execution (classpath -> D:\demo\ejb\CalculatorApplnClient.jar)
Stateful Session Bean
Does not
exists
remove()
Create() ejbRemove()
newInstance()
setSessionContext() ejbPassivate()
ejbCreate()
Ready Passive
ejbActivate()
Eg:
Problem Statement
Earnest Bank’s Chief Technology Officer (CTO) has entrusted the development
team with the task of creating an application to validate the credit card information
entered by the user.
The team is responsible for justifying the choice of stateful session bean as the
bean type and writing code for the required application
Eg:
Card.java
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface Card extends EJBObject
{
public int validate(String CardNo) throws RemoteException;
}
CardHome.java
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
CardEJB.java
import javax.ejb.*;
import java.rmi.RemoteException;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
1
Entity Bean
Problem Statement
Earnest Banks Chief Technology Officer (CTO) has entrusted the development team with
the task of creating an application that will enable the bank staff store the details of ATM
transactions carried out by an account holder, in the central SQL – Server 2000 database.
The staff should be able to store transaction details such as account id, transaction date,
transaction particulars, check number and amount withdrawn or deposited.
Database
Registration
cRegistrationID int
cFirst_Name char(50)
cLast_Name char(50)
cAddress char(50)
cAccount_tyoe char(30)
mAnnual_Income money(8)
cPhone_No char(10)
Account_Holder
cAccount_id char(10)
cRegistrationID int
mBalance money(8)
Account_Holder_Transaction
cAccount_id char(10)
dDate_of_Trans datetime
vcParticulars varchar(50)
cCheck_No char(10)
mAmount money