• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling SessionBean from Servlet

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sib Pete
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic