Title: Enterprise Java Beans
1Enterprise Java Beans
- Server Side Java Application Programming
2Overall structure
Database
Java Application
Middle Tier Server
Web Browser
File System
Mobile Phone
Legacy Application
3J2EE Server
J2EE Server
EJB Container Stateless Session Bean Stateful
Session Bean Entity Bean
Clients
Back end services
Web Container Servlet JSP Html/ Images
4Interacting with EJBs
- Normally servlets and JSPs will interact with
EJBs - However a client application can also directly
interact with EJBs
5J2EE Services
- EJB container provides
- Transaction Management
- Security
- Remote Client Connectivity
- Life Cycle Management
- Database Connection Pooling
6Enterprise Java Beans
- Contain the business logic of the application
- Session Beans
- Stateless Session Beans
- Stateful Session Beans
- Entity Beans
- Container Managed Persistence
- Bean Managed Persistence (Ejb 3 has no BMP)
7Session vs Entity Beans
8Session and Entity Use Example
J2EE Server
EJB Container
Database
Entity Bean
Entity Bean
Session Bean
Web Container
Web Browser
Servlet
9Structure of a J2EE Application
J2EE Application
J2EE App DD
.ear file
Web Component
Enterpries Bean
Web Comp DD JSP file Servlet class GIF/jpeg
File HTML File
EJB DD EJB class Remote class Local class
.jar file
.war file
10Standard Deployment Descriptors
- Enterprise Archive (ear) file
- application.xml inside the META-INF directory
- Java Archive (jar) file
- ejb-jar.xml inside the META-INF directory
- Web Archive (war) file
- web.xml inside the WEB-INF directory
11Application Server
- Netbeans 6 uses the Glassfish V2 Application
Server - Others can be used if wanted
- To deploy a J2EE application on Glassfish V2 we
need to add several vendor deployment descriptors - sun-web.xml inside the WEB-INF directory
- sun-ejb-jar.xml inside the META-INF directory
12Enterprise Beans
- All enterprise beans need
- Bean Class
- Implementation of the bean
- Local Interface
- Allows Clients to Connect in the same Virtual
Machine, very efficient - Remote Interface
- Allows Clients to connect using RMI/ IIOP, used
when client in different virtual machine.
13Session Beans
- Bean Class
- Implements the business method of the bean
- Local Interface
- Defines the business methods that a local client
can call. - Remote Interface
- Defines the business methods that a remote client
can call. - NOTE the business methods are implemented in the
Bean Class
14Session Bean Class
- Implements the SessionBean Interface
- Must define
- One or more ejbCreate methods
- ejbRemove()
- ejbActivate()
- ejbPassivate()
- setSessionContext(SessionContext sc)
- In Ejb3 implemented via _at_Stateless of
_at_Stateful annotation - Can define additional business methods
- Must Implement any Local and Remote Interfaces
15Session Bean Class Business Methods
- Addition business methods can be defined in the
Session Bean Class - public double convertL(double dollars)
- return dollars 1.95
- public double convertR(double dollars)
- return dollars 1.95
16Session Bean Local Interface
- Local Interface defines any business methods that
a local client may invoke - These methods signatures must match exactly those
business methods defined in the Session Bean
Class - public double convertL(double dollars)
17Session Bean Remote Interface
- Remote Interface defines any business methods
that a remote client may invoke - These methods signatures must match exactly those
business methods defined in the Session Bean
Class - public double convertR(double dollars)
18Packaging the Session Bean
- The Session Bean is packaged into a jar file
before deployment - The Netbeans IDE packages the bean including any
deployment descriptors - ejb-jar.xml
- sun-ejb-jar.xml
19Accessing and using the Session Bean (from a
Servlet)
- Declare the Session Bean using the _at_EJB
annotation - _at_EJB
- private sbConvLocal sbConvBean
- Use the Session Bean in the servlet code
- sbConvBean.convL(100)
20Stateful Session Bean Lifecycle
21Stateless Session Bean Lifecycle
22Summary
- Main points to remember
- J2EE Application server
- EJB container
- Entity and Session Beans
- Session Bean details
- Bean Class
- Home Interface
- Remote Interface