File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Calling SessionBean from  Servlet " Watch "Calling SessionBean from  Servlet " New topic

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

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

2)To inject a remote interface

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

Include in your web.xml DD:

Best regards,

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:
subject: Calling SessionBean from Servlet
jQuery in Action, 3rd edition