How to invoke EJB component from a servlet Readme file Servlet Sample Application

Date: 25/03/2004


Table of Contents

Introduction
Application Overview
Code Snippet
Sample Application Files
Setting up the Sample Application
Additional References

Introduction

Prerequisite

To work your way through this Sample Application, a basic understanding of the fundamental concepts of servlets, JSP, JDBC and EJB(Session and Entity Bean) are required. For more information please refer Additional References.

Technical Overview

Servlets provide a component-based, platform-independent method for building Web-based applications. They are generally used for generating dynamic Web content. In this sample, the servlet gets a request from a client such as a Web browser, interacts with the EJB to process the request, returns the response back to the client and displays the retrieved information on the client machine.

Enterprise JavaBeans (EJB) is a specification created by Sun Microsystems that defines a framework for server-side Java components. Implementations of the EJB specification can typically be found in application servers, which support the creation of e-business solutions. EJB makes distributed object technology more accessible and easier to use by offering an higher level of abstraction and therefore more efficient for software development. To know more about EJB refer the Additional References section at the end.

Application Overview 

This sample shows how a servlet can communicate with an EJB component to process a user request. In the current sample user enters employee id, this id will be passed to ClientServlet servlet. ClientServlet will locate EmployeeSession Session bean. EmployeeSession Session bean will locate Employee Entity bean to fetch the employee information from the database and to create employeeDetails object. EmployeeDetail object is a Synchronised object, which can be used to encapsulate employee information and to transfer employee information over the network.

Employee Entity bean is local to the session bean, hence EmployeeSession bean locates local home of the Employee Entity bean. The employeeDetails object will be passed back to the session bean. The Session bean will further pass employeeDetails object to the ClientServlet servlet. Finally the ClientServlet servlet performs the responsibility to display the employee information to the client. Following figure shows the application architecture.


                                             Image Not Available
                                                                            Architecture Diagram

Code Snippet

Calling Session bean from the servlet

Servlet need to perform the following steps to call session bean's function.
a) Acquire a JNDI Initial context.
b) Locate the Home object using JNDI.
c) Use home object to creat a the remote object of the session bean.
d) Make a call to the getEmployeeDetails(...) function by using the remote object.

Following code snippet illustrates, how to call session bean's method from the servlet.

ClientServlet.getEmployeeDetails( ... )

 
private EmployeeDetails getEmployeeDetails(String id)
throws EmployeeException, Exception {
// create the reference to EmployeeSession session bean
EmployeeSession employeeMgr = null;

// create the reference to EmployeeDetails object
EmployeeDetails detail = null;

// create the primary key
EmployeePK pk = new EmployeePK(id);

try {
// obtain the JNDI initial context
InitialContext ctx = new InitialContext();

// get a reference to a home object - the factory to the EmployeeSession
// remote object
Object obj = ctx.lookup("java:comp/env/EmployeeSession");

// Home object are RMI-IIOP object so they must be cast to
//RMI-IIOP object
EmployeeSessionHome home = (EmployeeSessionHome) PortableRemoteObject.narrow(obj,
EmployeeSessionHome.class);

// use factory to create the remote object.
employeeMgr = home.create();

// call getEmployeeDetails(...) method on the EJB object. The EJB object
// will delegate the call to the bean, receive the employee details
// and set it to the detail reference variable.
detail = employeeMgr.getEmployeeDetails(pk);
} catch (EmployeeException ex) {
// throw EmployeeException exception
throw ex;
} catch (Exception ex) {
// throw Exception exception
throw ex;
}

// return EmployeeDetails object
return detail;
}

Calling Entity bean from the Session bean

Now the session bean need to perform the following steps to call entity bean's function.
a) Acquire a JNDI Initial context.
b) Locate the LocalHome object of the Employee Entity bean using JNDI.
c) Use local home object to find and to creat the local object of the Employee Entity bean for the given employee id.
d) Make a call to the getEmployeeDetails(...) function by using the local object.

Following code snippet illustrates, how to call entity bean's method from the session bean.

EmployeeSessionBean.getEmployeeDetails( ... )

 
public EmployeeDetails getEmployeeDetails(EmployeePK pk)
throws RemoteException, EmployeeException, FinderException {
// create the reference variable of EmployeeLocal and EmployeeDetails class.
EmployeeLocal employee = null;
EmployeeDetails detail = null;

try {
//get the reference to the home object of Employee Entity bean.
EmployeeLocalHome home = getEmployeeLocalHome();

// find the employee for the given employee's primary key
employee = home.findByPrimaryKey(pk);

// get the employee details.
detail = employee.getEmployeeDetails();
} catch (FinderException ex) {
// if there is no employee for the given primary key ,throw
// FinderException exception.
throw new FinderException("Employee Id " + pk.toString() +
" is not found");
} catch (EmployeeException ex) {
// throw EmployeeException exception
throw new EmployeeException("Datasource in not located");
} catch (Exception ex) {
// throw EmployeeException exception
ex.printStackTrace();
throw new EmployeeException(ex.toString());
}

// return employee details.
return detail;
}


Sample Application Files 

Readme file and Stylesheets

Directory File Description
ServletToEJB/doc/ Readme.html This file gives introduction to the sample.
Install.html This file has instructions required to install and run the sample.

Java Server Page files for the Application

Directory File Description
ServletToEJB/web/
display.jsp This page displays the employee's detailed information for the given employee id.
errorHandler.jsp This JSP displays error page.
footer.html This is the page containing the footer information.
header.html This is the page containing the header information.
statusPage.jsp This page is shown if the employee information is not found in the database.
pagestyles.css This is the style sheet file.
welcome.jsp This is a welcome page where user will be asked to enter
employee id to see the detailed information for entered employee id.

ANT build Script

Directory File Description

ServletToEJB/

build.xml

This is an ANT build script.

build.properties Contains the parameters used by the ant to run the build.xml. user need to modify this property file.

Configuration files for the Application

Directory File Description

ServletToEJB/src/META-INF/

application.xml

Contains the application specific information.

data-sources.xml Contains the datasource information.This file will be used while deploying the sample by using ant.
ejb-jar.xml This is an EJB deployment descriptor file.
orion-application.xml Contains the OC4J specific information.

Java Source files for the Application

Directory File Description
ServletToEJB/src/oracle/otnsamples/servlet/ejb/ ClientServlet.java This is the servlet class, responsible to locate session bean and to create remote object of the located session bean and to call business method defined in the session bean.
EmployeePK.java This is a primary key class for the Employee.
EmployeeException.java This is a exception class thrown be the Employee.
EmployeeDetails.java This class is a Serializable class and will be used to keep employee information and to transfer employee information over the network.
EmployeeSessionHome.java This is the home interface for the EmployeeSessionBean. And serves as a factory for EJB (Session bean) objects.
EmployeeSession.java This interface is what ClientServlet operates on while interacting with the EJB(Session bean) object. Container will implement this interface, the implemented object is the EJB Object for the session bean, which delegates invocation to the actual bean (EmployeeSessionBean.java).
EmployeeSessionBean.java This is a state less session bean. Has a business method, getEmployeeDetails which accepts an employee primary key and returns the employee details of the given primary key.
EmployeeLocalHome.java This is a local home interface to the Employee bean. This interface will be implemented by the EJB container.
EmployeeLocal.java This is the local interface for the Employee bean. This interface is what Session bean operates on while the interacting with the Entity bean. The container will implement this interface. The implemented object will be called the local object, which delegates invocation the actual bean (EmployeeBean.java).
EmployeeBean.java This is BMP Entity bean and represents employee in an organization.

JDeveloper project files.

Directory File Description
ServletToEJB.jpr JDeveloper Project file
ServletToEJB.jws JDeveloper WorkSpace file
application.deploy Application deployment file
ejb.deploy EJB deployment file.
ServletToEJB.deploy Web application deployment file.

Setting up the Sample Application 

Refer to Install.html for step-by-step instructions on copying files, installing packages, and other configurations to successfully run the sample.

Additional References


We hope you find this README file helpful. Please enter your comments in the OTN Sample Code Discussion Forum.

Servlet to EJB Sample Application

Please rate this sample application :
Excellent
Good
Average
Below Average
Poor


E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy