Inside Scoop on J2EE : Tips and tricks on J2EE and Oracle Application Server by Debu Panda
Updated: 11/14/2005; 11:24:46 AM.

 

Subscribe to "Inside Scoop on J2EE" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.

 
 

Monday, October 24, 2005

Few readers wrote me asking how can they invoke EJB 3.0 session beans from web container. I have been helping one of the customers who have been trying to port an application from JBoss EJB 3.0 implementation to OC4J 10.1.3 DP4 and apparently they were using JBoss’s proprietary extension of looking by class name similar to OC4J’s ability to lookup by ejb-name specified in the deployment descriptor. It's worth mentioning that Java EE specification can ensure portability of applications only by explicitly defining the dependencies on EJBs by using ejb-ref or ejb-local-ref and then looking using the ref-name to lookup the EJB.

 

For example, if you want to lookup an EJB 3.0 session bean from a Servlet or JSP then you can define the dependency on the EJB as follows:

 

<ejb-local-ref>
        <ejb-ref-name>ejb/HelloWorld</ejb-ref-name>
        <local>oracle.ejb30.HelloWorld</local>
 </ejb-local-ref>

 

Then lookup the object using JNDI as follows before we can use the resource.

Context ic = new InitialContext();

 

HelloWorld helloWorld = (HelloWorld)ic.lookup("java:comp/env/ejb/HelloWorld");

helloWorld.sayHello("Debu");

 

If you are using EJB from another Session bean or MDB you may use dependency injection to get a reference to another EJB instead of you using JNDI lookup as follows:

 

@EJB
private  HelloWorld helloWorld;
helloWorld.sayHello("Debu");

 

We currently do not support dependency injection in web container and hoping Java EE 5.0 supports dependency injection in the web container you should be able use the above code in your Servlet classes in a Java EE 5.0 compliant container.


12:12:25 PM    comment []

© Copyright 2005 Debu Panda.

PS: These are my own thoughts and not of my employer ..



Click here to visit the Radio UserLand website.
 


October 2005
Sun Mon Tue Wed Thu Fri Sat
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Sep   Nov