Referencing External EJBs More about the ejb-ref Elements

7-2 Developing Web Applications, Servlets, and JSPs for Oracle WebLogic Server associated with the HTTP request. You can also access JMS module resources, such as queues, topics, and connection factories. For more information see, Configuring JMS Application Modules for Deployment in Configuring and Managing JMS for Oracle WebLogic Server. To configure resources: 1. Enter the resource name in the deployment descriptor as you use it in your code, the Java type, and the security authorization type. 2. Map the resource name to the JNDI name. The following example illustrates how to use an external data source. It assumes that you have defined a data source called accountDataSource. For more information, see JDBC Data Sources in Oracle WebLogic Server Administration Console Help. Example 7–1 Using an External DataSource servlet code: javax.sql.DataSource ds = javax.sql.DataSource ctx.lookup myDataSource; web.xml entries: resource-ref . . . res-ref-namemyDataSourceres-ref-name res-typejavax.sql.DataSourceres-type res-authCONTAINERres-auth . . . resource-ref weblogic.xml entries: resource-description res-ref-namemyDataSourceres-ref-name jndi-nameaccountDataSourcejndi-name resource-description

7.3 Referencing External EJBs

Web applications can access EJBs that are deployed as part of a different application a different EAR file by using an external reference. The EJB being referenced exports a name to the global JNDI tree in its weblogic-ejb-jar.xml deployment descriptor. An EJB reference in the Web application module can be linked to this global JNDI name by adding an ejb-reference-description element to its weblogic.xml deployment descriptor. This procedure provides a level of indirection between the Web application and an EJB and is useful if you are using third-party EJBs or Web applications and cannot modify the code to directly call an EJB. In most situations, you can call the EJB directly without using this indirection. For more information, see Programming WebLogic Enterprise JavaBeans for Oracle WebLogic Server. To reference an external EJB for use in a Web application: 1. Enter the EJB reference name you use to look up the EJB in your code, the Java class name and the class name of the home and remote interfaces of the EJB in the ejb-ref element of the J2EE standard deployment descriptor, web.xml. The web.xml file is located in the WEB-INF directory of your Web application. Configuring Resources in a Web Application 7-3 2. Map the reference name in the ejb-reference-description element of the WebLogic-specific deployment descriptor, weblogic.xml, to the JNDI name defined in the weblogic-ejb-jar.xml file. If the Web application is part of an Enterprise Application Archive EAR file, you can reference an EJB by the name used in the EAR with the ejb-link element of the J2EE standard deployment descriptor, web.xml.

7.4 More about the ejb-ref Elements

The ejb-ref element in the web.xml deployment descriptor declares that either a servlet or JSP is going to be using a particular EJB. The ejb-reference-description element in the weblogic.xml deployment descriptor binds that reference to an EJB, which is advertised in the global JNDI tree. The ejb-reference-descriptor element indicates which ejb-ref element it is resolving with the ejb-ref-name element. That is, the ejb-reference-descriptor and ejb-ref elements with the same ejb-ref-name element go together. With the addition of the ejb-link syntax, the ejb-reference-descriptor element is no longer required if the EJB being used is in the same application as the servlet or JSP that is using the EJB. The ejb-ref-name element serves two purposes in the web.xml deployment descriptor: ■ It is the name that the user code servlet or JSP uses to look up the EJB. Therefore, if your ejb-ref-name element is ejb1, you would perform a JNDI name lookup for ejb1 relative to java:compenv. The ejb-ref-name element is bound into the component environment java:compenv of the Web application containing the servlet or JSP. Assuming the ejb-ref-name element is ejb1, the code in your servlet or JSP should look like: Context ctx = new InitialContext; ctx = Contextctx.lookupjava:compenv; Object o = ctx.lookupejb1; Ejb1Home home = Ejb1Home PortableRemoteObject.narrowo, Ejb1Home.class; ■ It links the ejb-ref and ejb-reference-descriptor elements together.

7.5 Referencing Application-Scoped EJBs