Using An EJB From A Servlet by aof75410

VIEWS: 7 PAGES: 2

									                                Using An EJB From A Servlet

This white paper provides a step-by-step approach for correctly configuring a Servlet to access an EJB in
WebLogic Server 6.1.

1. The EJB developer will already have created the EJB. Determine the EJB’s JNDI name by looking
   in its weblogic-ejb-jar.xml file. In this example, the JNDI name is “ejb.CMPService”.

    <weblogic-enterprise-bean>
         <ejb-name>CMPService</ejb-name>
         …
         <jndi-name>ejb.CMPService</jndi-name>
    </weblogic-enterprise-bean>


2. Create an EJB reference entry in the Web Application’s weblogic.xml file. The “jndi-name” found
   in step 1 should be used to populate the “jndi-name” entry below.

    The naming convention for the “jndi-name” above, and the “ejb-ref-name” in the entry below is
    specified in the J2EE 1.3 Specification. It suggests that all EJB related names should start with
    “ejb”. This information can be found in section 5.3.1.1 of the specification at:
    http://java.sun.com/j2ee/j2ee-1_3-fr-spec.pdf.

    <reference-descriptor>
         <ejb-reference-description>
             <ejb-ref-name>ejb/CMPService</ejb-ref-name>
             <jndi-name>ejb.CMPService</jndi-name>
         </ejb-reference-description>
    </reference-descriptor>


3. Create an EJB reference entry in the Web Application’s web.xml file.

    <ejb-ref>
         <description>CMP Facade</description>
         <ejb-ref-name>ejb/CMPService</ejb-ref-name>
         <ejb-ref-type>Session</ejb-ref-type>
         <home>ejb20.session.CMPServiceHome</home>
         <remote>ejb20.session.CMPService</remote>
    </ejb-ref>




Dave Robinson                                  January 7, 2002                                          1 of 2
robinson_dave@yahoo.com
                                Using An EJB From A Servlet


4. Refer to the EJB in the Servlet’s source code.

    …

    try {
       Context context = new InitialContext();
       Object ref = context.lookup("java:comp/env/ejb/CMPService");
       CMPServiceHome home = (CMPServiceHome)
             PortableRemoteObject.narrow(ref, CMPServiceHome.class);
       CMPService service = home.create();
       …
    } catch (Exception e) {
       …
    }

    …




Dave Robinson                                 January 7, 2002          2 of 2
robinson_dave@yahoo.com

								
To top