Download EJB - Elte

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Integrating J2EE Components
Copyright © 2004, Oracle. All rights reserved.
Objectives
After completing this lesson, you should be able to do
the following:
• Create JavaServer Pages (JSP) clients for EJBs
• Use Enterprise JavaBeans (EJB) tags in JSPs
• Modify the configuration files in OracleAS 10g
Containers for J2EE (OC4J)
17-2
Copyright © 2004, Oracle. All rights reserved.
Overview
Web tier
JSP
HttpServlet
Request
EJB tier
Controller
servlet
To EIS
HttpServlet
Response
17-3
Employees
JSP
Copyright © 2004, Oracle. All rights reserved.
Employees
EJB
Creating Remote Clients for EJBs
To create a remote JSP client for an Enterprise
JavaBean, perform the following steps :
1. Import the EJB home interface.
2. Override the jspInit() method.
– Use the lookup() method to create a reference to
the EJB.
– Create the EJB remote object.
3. Retrieve the necessary parameters from the
request object and pass the parameters to the
EJB method.
4. Define a reference to the EJB in the web.xml file.
17-4
Copyright © 2004, Oracle. All rights reserved.
Importing the EJB Home Interface
Use the page directive to import the naming package
and the home interface for the EJB:
<%@ page import="mypackage.Employees,
mypackage.EmployeesHome, javax.ejb.*,
javax.naming.*,
javax.rmi.PortableRemoteObject,
java.rmi.RemoteException" %>
17-5
Copyright © 2004, Oracle. All rights reserved.
Create a Reference to the EJB
To create an EJB reference, use the lookup method.
Override the jspInit() method as follows:
<%! Employees employees = null;
public void jspInit() {
try {
InitialContext ic = new InitialContext();
EmployeesHome employeesHome = (EmployeesHome)
PortableRemoteObject.narrow
(ic.lookup("java:comp/env/Employees"),
EmployeesHome.class);
employees = employeesHome.create(); }
catch (RemoteException ex) {
ex.printStackTrace();
}
}
%>
17-6
Copyright © 2004, Oracle. All rights reserved.
Passing Arguments to the EJB Method
Calling an EJB method from a JSP expression
evaluates the return value of the EJB method and
displays it, as follows:
<html><body><h1>Insert an Employee Number:</h1>
<form method="get">
<p><input type="text" name="empno">
<p><input type="Submit" value="Submit">
</form>
<% String empno = request.getParameter("empno");
if (empno != null && empno.length() >0) {
int i = Integer.parseInt(empno);%>
<%= employees.getDetails(i) %>
<% } %>
</body></html>
17-7
Copyright © 2004, Oracle. All rights reserved.
Creating an EJB Reference
To obtain an EJB reference for the Web tier
components, create an <ejb-ref> element for the
EJB in the web.xml file:
17-8
Copyright © 2004, Oracle. All rights reserved.
Creating Local Clients for EJBs
Creating JSP clients for local EJBs is similar to
creating remote clients, with the following exceptions:
• The EJB object does not have to be cast to a
PortableRemoteObject.
• Because the create() method does not throw a
RemoteException exception, this does not have
to be caught in the client.
• Instead of <ejb-ref>, an <ejb-local-ref>
element is created in the client web.xml file.
17-9
Copyright © 2004, Oracle. All rights reserved.
ejb-local-ref Element
Right-click web.xml to create an EJB local reference.
17-10
Copyright © 2004, Oracle. All rights reserved.
EJB Tags
Oracle Application Server 10g provides a tag library
that contains custom tags for referencing EJBs in
JSPs:
17-11
EJB Tag
Purpose
useHome
Looks up the home interface for the EJB and
creates an instance of it
useBean
Instantiates the EJB
createBean
Nests an EJB createBean tag within the
useBean tag to create the EJB instance
iterate
Iterates through a collection of EJB
instances
Copyright © 2004, Oracle. All rights reserved.
useHome Tag
The useHome tag has no body. The attributes are:
• id (required)
• type (required)
• location (required)
<EJB:useHome id="empHome"
type="mypackage.EmployeesHome"
location="java:comp/env/ejb/Employees"
local="false" />
17-12
Copyright © 2004, Oracle. All rights reserved.
useBean Tag
The attributes of the useBean tag include:
• id (required)
• type (required)
• value
• scope
<EJB:useBean id="bean"
type="mypackage.EmployeesBean"
scope="session" />
17-13
Copyright © 2004, Oracle. All rights reserved.
createBean Tag
The createBean tag contains only one required
attribute named instance.
<EJB:useBean id="bean"
type="mypackage.EmployeesBean"
scope="session">
<EJB:createBean
instance="<%=EmployeesHome.create()%>" />
</EJB:useBean>
17-14
Copyright © 2004, Oracle. All rights reserved.
iterate Tag
The following are the attributes of the iterate tag:
• id (required)
• type (required)
• collection (required)
• max
<EJB:iterate id="empdetails"
type="mypackage.Employees"
collection="<%=bean.getDetails(pk)%>"
max="100"> <jsp:getProperty name="empdetails"
property="id" /> </EJB:iterate>
17-15
Copyright © 2004, Oracle. All rights reserved.
Using the EJB Tags
To use the EJB tags, perform the following steps:
1. Add the Oracle9iAS library to your project in
JDeveloper.
a. Double-click the project and select libraries.
b. Make sure that “Oracle9iAS” is included in your
project.
2. Use the OJSP EJB Library provided in the
component palette to add the necessary tags to
your JSP file. (This adds the bean as well as the
taglib directive to the JSP.)
17-16
Copyright © 2004, Oracle. All rights reserved.
Deploying an Application: Web Tier
To deploy the Web tier components of a Java 2,
Enterprise Edition (J2EE) application, perform the
following steps:
1. Make sure that the ejb-ref or ejb-local-ref
elements exist in the web.xml file.
2. Create a WAR deployment profile (.deploy) in
JDeveloper.
3. Right-click the .deploy file and select either of
the following:
– Deploy to WAR file
– Deploy to OracleAS10g (where OracleAS10g is the
name of your application server connection)
17-17
Copyright © 2004, Oracle. All rights reserved.
Deploying an Application: EJB Tier
1. Make sure that the ejb-jar.xml file contains
unique mappings for each EJB in the application.
2. Create a JAR deployment profile (.deploy) in
JDeveloper.
3. Right-click the .deploy file and select either of
the following:
– Deploy to JAR file
– Deploy to OracleAS10g
17-18
Copyright © 2004, Oracle. All rights reserved.
Deploying an Application: EAR File
1. Create an EAR file in JDeveloper.
2. Specify the EJB JAR and WAR files that are to be
included in this EAR file.
3. Right-click the .deploy file and select either of
the following:
– Deploy to EAR file
– Deploy to OracleAS10g
17-19
Copyright © 2004, Oracle. All rights reserved.
Deploying from Oracle Enterprise Manager
From the Enterprise Manager home page, click Home,
then Application, and then select Deploy EAR file:
17-20
Copyright © 2004, Oracle. All rights reserved.
Summary
In this lesson, you should have learned how to:
• Create JSP clients for EJBs
• Modify deployment descriptors for looking up an
EJB client
• Deploy a J2EE application to Oracle Application
Server 10g
17-21
Copyright © 2004, Oracle. All rights reserved.
Practice 17-1 and 17-2: Overview
These practices cover the following topics:
• Creating JSP clients that access EJBs
• Deploying a Web application to Oracle Application
Server 10g
17-22
Copyright © 2004, Oracle. All rights reserved.