1 / 22

Integrating J2EE Components

Integrating J2EE Components. 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 10 g Containers for J2EE (OC4J). Overview.

kylar
Download Presentation

Integrating J2EE Components

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Integrating J2EE Components

  2. 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)

  3. Overview Web tier EJB tier HttpServletRequest JSP Controller servlet To EIS Employees EJB Employees JSP HttpServletResponse

  4. 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.

  5. 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" %>

  6. 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(); } } %>

  7. 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>

  8. 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:

  9. 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.

  10. ejb-local-ref Element • Right-click web.xml to create an EJB local reference.

  11. Purpose EJB Tag useHome Looks up the home interface for the EJB and creates an instance of it useBean Instantiates the EJB Nests an EJB createBean tag within the useBean tag to create the EJB instance createBean iterate Iterates through a collection of EJB instances EJB Tags • Oracle Application Server 10g provides a tag library that contains custom tags for referencing EJBs in JSPs:

  12. 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" />

  13. useBean Tag • The attributes of the useBean tag include: • id (required) • type (required) • value • scope <EJB:useBean id="bean" type="mypackage.EmployeesBean" scope="session" />

  14. 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>

  15. 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>

  16. 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. 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)

  18. 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

  19. 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

  20. Deploying from Oracle Enterprise Manager • From the Enterprise Manager home page, click Home, then Applications, and then select Deploy EAR file:

  21. 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

  22. 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

More Related