SlideShare a Scribd company logo
1 of 16
EJB
Creating first Session
Bean
Session 1
Introduction to EJB
1. EJB provides component base Development
2. Provide write once deploy anywhere
3. Allows customization via Deployment Descriptor (no Java code
modification)
4. Development focuses on Business Logic
5. EJB Server provides
1. Transaction Mgmt
2. Security
3. Concurrency
4. Resource Mgmt
5. Messaging
6. Depoly time Customization
Types of Bean
1. Entity Bean
• Represent a thing in persistence store e.g. row on database or
Serialized object in file.
2. Message Driven Bean
• Can listen a message from JMS Messaging service
3. Session Bean
• Represent a process or can contain a business logic
• Stateful Session Bean
• Can remember conversational state between method call
• Stateless Session Bean
• Can’t remember conversational state between method call
Rules for Building a Sample Bean
To create sample bean we need to follow below steps
• Code the bean class with all business method
• Code to interfaces for bean (home & component)
• Create XML Deployment Descriptor, must be
name it as ejb-jar.xml
• Deploy bean in application server
Bean Class HelloBean.java
package com;
import javax.ejb.*;
public class HelloBean implements SessionBean{
public void ejbActivate(){}
public void ejbPassivate(){}
public void ejbRemove(){}
public void setSessionContext(SessionContext ctx){}
public String helloWorld(){
return "Hello there!!!";
}
public void ejbCreate() {}
}
Little (More) about Bean Class HelloBean.java
If you are completely new to EJB, then you definitely says
what the hec is that… here is your answers
1. Your bean class (which contains the actual definition of mth
or says business logic) must implements SessionBean
interface according to spec
2. According to java rule you have to implements all
unimplemented mth from interface
3. The helloWorld() is your actual business mth, which will be
called by client
4. The ejbCreate() not come from SessionBean interface, but
we need to give the empty definition of it (???WTF??? this
is what is going is your mind right now, b relax the answer is
coming in next few slides.
Component Interface Hello.java
package com;
import javax.ejb.*;
import java.rmi.*;
public interface Hello extends EJBObject{
public String helloWorld() throws RemoteException;
}
Little (More) about Component Interface
1. You definitely find some similarities between Component
Interface & Bean class, if you are really thinking what I m
thinking, then you are right.
2. The component interface contains declarations of all
business mth which is to be defined by Bean class.
3. But according to java rules to implement the mth from
interface, the class must have the these magical statements
“implements Interface_Name”, which is in our case is
missing. Then how these two are related. Forget this for
now, I will explain this later.
4. Component interface must extends EJBObject interface
5. Its all mth must throws RemoteException
6. They can also thorws custom application (Checked)
Exception.
7. Its return type must be primitives/Serializable
Objs/Arrays/Collection of primitives or Serializable
Objs/Remote obj (These are called as RMI-IIOP
Complients).
Home Interface HelloHome.java
package com;
import javax.ejb.*;
import java.rmi.*;
public interface HelloHome extends EJBHome{
public Hello create() throws
CreateException,RemoteException;
}
Little (More) about Component Interface
• Home interface must extends EJBHome interface
• Must declare the create() (here is hint for your Bean class
ejbCreate() mth)
• Return type of create() must be component interface type
• Mth must throws CreateException & RemoteException.
• They can also thorws custom application (Checked)
Exception.
Combining the all stuff together
• Now what we have here : Component, Home & bean. we know
all these stuff work together, but question is how? Here is
ur ans.
• We need to tell the container that all these things are work
together by putting their entries in ejb-jar.xml
• WTH is ejb-jar.xml? : It is Deployment Descriptor (DD) like
web.xml in servlets.
• It tells the container that which is home, component & bean
to run the business logic.
• It also states what the Bean type i.e. Session(stateless,
stateful), Entity or Message-Driven (You can understand all
this from syntax).
• We are creating only sample stateless session bean, so only
session tags are avail. For Entity & Message-Driver will see
later.
Sample ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar>
<display-name>Hello EJB App</display-name>
<enterprise-beans>
<session>
<display-name>Hello</display-name>
<ejb-name>Hello</ejb-name>
<home>com.HelloHome</home>
<remote>com.Hello</remote>
<ejb-class>com.HelloBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
Deploying & Calling the Bean mth
• Now you have to create the jar of all compiled class with
below structure.
ejbApp.jar
com
Hello.class
HelloHome.class
HelloBean.class
META-INF
ejb-jar.xmlejb-jar.xml
glassfish-ejb-jar.xml
Deploying & Calling the Bean mth
• You noticed there is one more xml file glassfih-ejb-jar.xml,
now lets see what is it.
• As per my understanding, every server has its own ejb-
jar.xml file for deployment of ejb application. For e.g. if you
are deploying in weblogic server then you have additional
weblogic-ejb-jar.xml
• As I m using glassfish server for app deployment so I have
glassfish-ejb-jar.xml
• We will see the deployments of ejb & web application (part)
in later.
• Now I am writing the plane jsp to call the Bean mth.
• You need to deploy this jsp(war) file in glassfish & run the
jsp.
Client HelloClient.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="javax.rmi.PortableRemoteObject"%>
<%@page import="com.HelloHome"%>
<%@page import="com.Hello"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<%
try{
InitialContext ic = new InitialContext();
Object obj = ic.lookup("Hello");
HelloHome home =
(HelloHome)PortableRemoteObject.narrow(obj,HelloHome.class);
Hello hello = home.create();
String res = hello.helloWorld();
System.out.println(res);
out.println(res);
}catch(Exception e){
e.printStackTrace();
}
%>
</body>
</html>
Thank you
• I am not expert in EJB, but if you have any doubts in
tutorial please feel free to contact me on
ashishkirpan@gmail.com
• Sorry for spelling & grammar mistakes :)
Have a fun with EJB

More Related Content

What's hot (18)

Ejb notes
Ejb notesEjb notes
Ejb notes
 
Java bean
Java beanJava bean
Java bean
 
EJB 3.0 - Yet Another Introduction
EJB 3.0 - Yet Another IntroductionEJB 3.0 - Yet Another Introduction
EJB 3.0 - Yet Another Introduction
 
EJB 3.0 Java Persistence with Oracle TopLink
EJB 3.0 Java Persistence with Oracle TopLinkEJB 3.0 Java Persistence with Oracle TopLink
EJB 3.0 Java Persistence with Oracle TopLink
 
Enterprise JavaBeans(EJB)
Enterprise JavaBeans(EJB)Enterprise JavaBeans(EJB)
Enterprise JavaBeans(EJB)
 
Entity beans in java
Entity beans in javaEntity beans in java
Entity beans in java
 
Enterprise java beans(ejb) update 2
Enterprise java beans(ejb) update 2Enterprise java beans(ejb) update 2
Enterprise java beans(ejb) update 2
 
EJB 2
EJB 2EJB 2
EJB 2
 
enterprise java bean
enterprise java beanenterprise java bean
enterprise java bean
 
Enterprise Java Beans( E)
Enterprise  Java  Beans( E)Enterprise  Java  Beans( E)
Enterprise Java Beans( E)
 
Introduction to EJB
Introduction to EJBIntroduction to EJB
Introduction to EJB
 
Java EE EJB Applications
Java EE EJB ApplicationsJava EE EJB Applications
Java EE EJB Applications
 
Enterprise java beans
Enterprise java beansEnterprise java beans
Enterprise java beans
 
Jsf+ejb 50
Jsf+ejb 50Jsf+ejb 50
Jsf+ejb 50
 
Session 1 Tp1
Session 1 Tp1Session 1 Tp1
Session 1 Tp1
 
Ch4 ejb
Ch4 ejbCh4 ejb
Ch4 ejb
 
EJB Interview Questions
EJB Interview QuestionsEJB Interview Questions
EJB Interview Questions
 
Ejb intro
Ejb introEjb intro
Ejb intro
 

Similar to 1-introduction to ejb

Session 3 Tp3
Session 3 Tp3Session 3 Tp3
Session 3 Tp3phanleson
 
Enterprise java beans(ejb)
Enterprise java beans(ejb)Enterprise java beans(ejb)
Enterprise java beans(ejb)vikram singh
 
Enterprise java beans(ejb) Update 2
Enterprise java beans(ejb) Update 2Enterprise java beans(ejb) Update 2
Enterprise java beans(ejb) Update 2vikram singh
 
Session 4 Tp4
Session 4 Tp4Session 4 Tp4
Session 4 Tp4phanleson
 
J2EE - Practical Overview
J2EE - Practical OverviewJ2EE - Practical Overview
J2EE - Practical OverviewSvetlin Nakov
 
Real world java_ee_patterns
Real world java_ee_patternsReal world java_ee_patterns
Real world java_ee_patternsAlassane Diallo
 
ADVANCED JAVA MODULE I & II.ppt
ADVANCED JAVA MODULE I & II.pptADVANCED JAVA MODULE I & II.ppt
ADVANCED JAVA MODULE I & II.pptrani marri
 
Bea weblogic job_interview_preparation_guide
Bea weblogic job_interview_preparation_guideBea weblogic job_interview_preparation_guide
Bea weblogic job_interview_preparation_guidePankaj Singh
 
Ejb - september 2006
Ejb  - september 2006Ejb  - september 2006
Ejb - september 2006achraf_ing
 
Introduction to Java EE EJB Component
Introduction to Java EE EJB ComponentIntroduction to Java EE EJB Component
Introduction to Java EE EJB ComponentGanesh P
 
J boss ide-tutorial
J boss ide-tutorialJ boss ide-tutorial
J boss ide-tutorialUTN
 
Aravind vinnakota ejb_architecture
Aravind vinnakota ejb_architectureAravind vinnakota ejb_architecture
Aravind vinnakota ejb_architecturetayab4687
 
Contextual Dependency Injection for Apachecon 2010
Contextual Dependency Injection for Apachecon 2010Contextual Dependency Injection for Apachecon 2010
Contextual Dependency Injection for Apachecon 2010Rohit Kelapure
 
The Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans TechnologyThe Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans TechnologySimon Ritter
 

Similar to 1-introduction to ejb (20)

Session 3 Tp3
Session 3 Tp3Session 3 Tp3
Session 3 Tp3
 
Enterprise java beans(ejb)
Enterprise java beans(ejb)Enterprise java beans(ejb)
Enterprise java beans(ejb)
 
Enterprise java beans(ejb) Update 2
Enterprise java beans(ejb) Update 2Enterprise java beans(ejb) Update 2
Enterprise java beans(ejb) Update 2
 
Session 4 Tp4
Session 4 Tp4Session 4 Tp4
Session 4 Tp4
 
J2 Ee Overview
J2 Ee OverviewJ2 Ee Overview
J2 Ee Overview
 
Java j2eeTutorial
Java j2eeTutorialJava j2eeTutorial
Java j2eeTutorial
 
J2EE - Practical Overview
J2EE - Practical OverviewJ2EE - Practical Overview
J2EE - Practical Overview
 
Real world java_ee_patterns
Real world java_ee_patternsReal world java_ee_patterns
Real world java_ee_patterns
 
ADVANCED JAVA MODULE I & II.ppt
ADVANCED JAVA MODULE I & II.pptADVANCED JAVA MODULE I & II.ppt
ADVANCED JAVA MODULE I & II.ppt
 
Unite5-EJB-2019.ppt
Unite5-EJB-2019.pptUnite5-EJB-2019.ppt
Unite5-EJB-2019.ppt
 
Introcution to EJB
Introcution to EJBIntrocution to EJB
Introcution to EJB
 
What's new in Java EE 6
What's new in Java EE 6What's new in Java EE 6
What's new in Java EE 6
 
Bea weblogic job_interview_preparation_guide
Bea weblogic job_interview_preparation_guideBea weblogic job_interview_preparation_guide
Bea weblogic job_interview_preparation_guide
 
Ejb - september 2006
Ejb  - september 2006Ejb  - september 2006
Ejb - september 2006
 
Introduction to Java EE EJB Component
Introduction to Java EE EJB ComponentIntroduction to Java EE EJB Component
Introduction to Java EE EJB Component
 
J boss ide-tutorial
J boss ide-tutorialJ boss ide-tutorial
J boss ide-tutorial
 
Aravind vinnakota ejb_architecture
Aravind vinnakota ejb_architectureAravind vinnakota ejb_architecture
Aravind vinnakota ejb_architecture
 
Ejb intro
Ejb introEjb intro
Ejb intro
 
Contextual Dependency Injection for Apachecon 2010
Contextual Dependency Injection for Apachecon 2010Contextual Dependency Injection for Apachecon 2010
Contextual Dependency Injection for Apachecon 2010
 
The Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans TechnologyThe Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans Technology
 

Recently uploaded

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 

Recently uploaded (20)

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 

1-introduction to ejb

  • 2. Introduction to EJB 1. EJB provides component base Development 2. Provide write once deploy anywhere 3. Allows customization via Deployment Descriptor (no Java code modification) 4. Development focuses on Business Logic 5. EJB Server provides 1. Transaction Mgmt 2. Security 3. Concurrency 4. Resource Mgmt 5. Messaging 6. Depoly time Customization
  • 3. Types of Bean 1. Entity Bean • Represent a thing in persistence store e.g. row on database or Serialized object in file. 2. Message Driven Bean • Can listen a message from JMS Messaging service 3. Session Bean • Represent a process or can contain a business logic • Stateful Session Bean • Can remember conversational state between method call • Stateless Session Bean • Can’t remember conversational state between method call
  • 4. Rules for Building a Sample Bean To create sample bean we need to follow below steps • Code the bean class with all business method • Code to interfaces for bean (home & component) • Create XML Deployment Descriptor, must be name it as ejb-jar.xml • Deploy bean in application server
  • 5. Bean Class HelloBean.java package com; import javax.ejb.*; public class HelloBean implements SessionBean{ public void ejbActivate(){} public void ejbPassivate(){} public void ejbRemove(){} public void setSessionContext(SessionContext ctx){} public String helloWorld(){ return "Hello there!!!"; } public void ejbCreate() {} }
  • 6. Little (More) about Bean Class HelloBean.java If you are completely new to EJB, then you definitely says what the hec is that… here is your answers 1. Your bean class (which contains the actual definition of mth or says business logic) must implements SessionBean interface according to spec 2. According to java rule you have to implements all unimplemented mth from interface 3. The helloWorld() is your actual business mth, which will be called by client 4. The ejbCreate() not come from SessionBean interface, but we need to give the empty definition of it (???WTF??? this is what is going is your mind right now, b relax the answer is coming in next few slides.
  • 7. Component Interface Hello.java package com; import javax.ejb.*; import java.rmi.*; public interface Hello extends EJBObject{ public String helloWorld() throws RemoteException; }
  • 8. Little (More) about Component Interface 1. You definitely find some similarities between Component Interface & Bean class, if you are really thinking what I m thinking, then you are right. 2. The component interface contains declarations of all business mth which is to be defined by Bean class. 3. But according to java rules to implement the mth from interface, the class must have the these magical statements “implements Interface_Name”, which is in our case is missing. Then how these two are related. Forget this for now, I will explain this later. 4. Component interface must extends EJBObject interface 5. Its all mth must throws RemoteException 6. They can also thorws custom application (Checked) Exception. 7. Its return type must be primitives/Serializable Objs/Arrays/Collection of primitives or Serializable Objs/Remote obj (These are called as RMI-IIOP Complients).
  • 9. Home Interface HelloHome.java package com; import javax.ejb.*; import java.rmi.*; public interface HelloHome extends EJBHome{ public Hello create() throws CreateException,RemoteException; }
  • 10. Little (More) about Component Interface • Home interface must extends EJBHome interface • Must declare the create() (here is hint for your Bean class ejbCreate() mth) • Return type of create() must be component interface type • Mth must throws CreateException & RemoteException. • They can also thorws custom application (Checked) Exception.
  • 11. Combining the all stuff together • Now what we have here : Component, Home & bean. we know all these stuff work together, but question is how? Here is ur ans. • We need to tell the container that all these things are work together by putting their entries in ejb-jar.xml • WTH is ejb-jar.xml? : It is Deployment Descriptor (DD) like web.xml in servlets. • It tells the container that which is home, component & bean to run the business logic. • It also states what the Bean type i.e. Session(stateless, stateful), Entity or Message-Driven (You can understand all this from syntax). • We are creating only sample stateless session bean, so only session tags are avail. For Entity & Message-Driver will see later.
  • 12. Sample ejb-jar.xml <?xml version="1.0" encoding="UTF-8"?> <ejb-jar> <display-name>Hello EJB App</display-name> <enterprise-beans> <session> <display-name>Hello</display-name> <ejb-name>Hello</ejb-name> <home>com.HelloHome</home> <remote>com.Hello</remote> <ejb-class>com.HelloBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> </ejb-jar>
  • 13. Deploying & Calling the Bean mth • Now you have to create the jar of all compiled class with below structure. ejbApp.jar com Hello.class HelloHome.class HelloBean.class META-INF ejb-jar.xmlejb-jar.xml glassfish-ejb-jar.xml
  • 14. Deploying & Calling the Bean mth • You noticed there is one more xml file glassfih-ejb-jar.xml, now lets see what is it. • As per my understanding, every server has its own ejb- jar.xml file for deployment of ejb application. For e.g. if you are deploying in weblogic server then you have additional weblogic-ejb-jar.xml • As I m using glassfish server for app deployment so I have glassfish-ejb-jar.xml • We will see the deployments of ejb & web application (part) in later. • Now I am writing the plane jsp to call the Bean mth. • You need to deploy this jsp(war) file in glassfish & run the jsp.
  • 15. Client HelloClient.jsp <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <%@page import="javax.naming.InitialContext"%> <%@page import="javax.rmi.PortableRemoteObject"%> <%@page import="com.HelloHome"%> <%@page import="com.Hello"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> <% try{ InitialContext ic = new InitialContext(); Object obj = ic.lookup("Hello"); HelloHome home = (HelloHome)PortableRemoteObject.narrow(obj,HelloHome.class); Hello hello = home.create(); String res = hello.helloWorld(); System.out.println(res); out.println(res); }catch(Exception e){ e.printStackTrace(); } %> </body> </html>
  • 16. Thank you • I am not expert in EJB, but if you have any doubts in tutorial please feel free to contact me on ashishkirpan@gmail.com • Sorry for spelling & grammar mistakes :) Have a fun with EJB