aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Calling SessionBean from  Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Calling SessionBean from  Servlet " Watch "Calling SessionBean from  Servlet " New topic
Author

Calling SessionBean from Servlet

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

I am using Weblogic11g server .

In my application I am calling SessionBean (EJB3.0) from my servlet using MappedName#PackageName.RemoteBean Name , its working fine .

But Can you please tell me if i can use @EJB in servlets for this purpose .

Thanks in advance


Save India From Corruption - Anna Hazare.
Sib Pete
Greenhorn

Joined: Apr 27, 2009
Posts: 26
My understanding is that as long as the servlets and ejb beans are running collocated on the same jvm, then you can use the @EJB annotation. However, if the servlets and ejb beans are running on different jvms then you need to use JNDI lookup.

Basically, if your invoking local interface on the ejb I think you can use the @EJB annotation and if you are using the remote interface you have to use the JNDI.
Manuel Alberto Quero
Ranch Hand

Joined: Jul 17, 2009
Posts: 31
Hello Sib,

I disagree with you a little bit...

I think that Dependency Injection (in our case @EJB) does not have to do with JVM. The responsible for understanding @EJB annotation is EJB container and servlets do not live in EJB containers, they live in servlets containers. So I do not think you can access your EJB from your servlet via @EJB annotation.
On the other hand, I think Dependency injection can be used with both local and remote interfaces as long as you are in the same EJB container (I know it doesn´t have much sense to have remote interfaces within the same EJB container, but you could do it).

Hope it helps,

Manuel
Manuel Alberto Quero
Ranch Hand

Joined: Jul 17, 2009
Posts: 31
Hello again,

I was wrong about my first statement. You can inject EJBs in your servlet by using @EJB.
Indeed, you can inject both local and remote interfaces.
You need to do the following:

1)To inject a local interface:

Include in your servlet:
@EJB(name = "ejb/ExampleSessionLocal")
private ExampleSessionLocal ejb;

Include in your web.xml DD:
<ejb-local-ref>
<ejb-ref-name>ejb/ExampleSessionLocal</ejb-ref-name>
<local>com.company.app.ExampleSessionLocal</local>
</ejb-local-ref>


2)To inject a remote interface

Include in your servlet:
@EJB(name = "ejb/ExampleSessionRemote")
private ExampleSessionRemote ejb;

Include in your web.xml DD:
<ejb-ref>
<ejb-ref-name>ejb/ExampleSessionRemote</ejb-ref-name>
<remote>com.company.app.ExampleSessionRemote</remote>
</ejb-ref>


Best regards,

Manuel
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
As far as I know, dependency injection is a user friendly abstraction for jndi lookup, available to a select 'managed' classes. And servlet is deemed to be a managed class per Jave EE5. So as you can do a jndi lookup for an EJB from a servlet, you can access it via dependency injection as well(assuming that the container considers the servelet as manged, Jboss-4 does not).


Thanks and Regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling SessionBean from Servlet
 
Similar Threads
How to write unit test for a Stateless Session Bean Method
Load a configuration file
Load a configuration file
iPlanet Exception Handling
ManagedBean with SessionBean?