• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Injecting EJB's using DI

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,

As per the specification, the EJB annotation can be used only in managed classes of the EJB container. Can I inject a EJB resource in a Servlet? The EJB specification for the EJB container does not say that it should support Servlets & JSP. Any ideas?
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jothi Shankar Kumar wrote: Can I inject a EJB resource in a Servlet?



Yes, you can.
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yes, you can.


But this is new to the Servlet 3.0 API, right? And to my knowledge you can define EJB resource to be available to concurrently used components like Servlets but you still have to look it up via JNDI. Please correct me if I'm wrong!

Marco
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marco Ehrentreich wrote:

Yes, you can.


But this is new to the Servlet 3.0 API, right?



Not really. It was available even in JavaEE5 (which was Servlet 2.x).
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marco Ehrentreich wrote: And to my knowledge you can define EJB resource to be available to concurrently used components like Servlets but you still have to look it up via JNDI. Please correct me if I'm wrong!

Marco



Sorry, i can't seem to understand this question Can you please elaborate?
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's read this in Servlet 3.0Final Spec, 15.5.3 @EJBs Annotation

It says basically that you can annotate Servlet classes with one or more @EJB annotations just to declare that some EJBs should be available to this servlet class. But you still have to actively get a reference to the allowed EJBs via JNDI. It also says that this is just a convenient way to avoid that you have to declare the accessible EJBs in the web.xml descriptor.

Above this in the spec is an example with only one @EJB injection into a member variable in a servlet where it notes nothing about JNDI. Maybe the JNDI lookup is only necessary if you declare multiple @EJBs which wouldn't make much sense to me

Sorry that I can't tell you more about this but I don't really understand it in detail from this short explanation in the spec. Maybe you're willing to take the time to have a look into the specification yourself...

Marco
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that the @EJBs annotation is different from the @EJB annotation. The section 15.5.3 of the servlet spec talks about the @EJBs annotation. The @EJBs annotation is used to make available one or more Enterprise Java Beans in the java:comp/env namespace of a component (in this case the servlet). This is unlike the @EJB annotation which *injects* a bean reference in a field. The @EJBs are used to just make the beans available in the java:comp/env namespace of the servlet so that later on, in the servlet component you can do something like:


If you are familiar with the usage of <ejb-ref> in web.xml, then the @EJBs are equivalent.
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, obviously I didn't get the point with the "s" in the name of the annotation when I read the spec document last night But thanks a lot for pointing me to this difference!

There's still one thing I don't understand completely. It's not allowed to inject stateful EJBs into stateless ones because the container couldn't guarantee that a client sees the correct injected EJB when SLSBs are shared between different clients. How does this work for Servlets which are usually reused and shared between multiple clients, too? Or are there restrictions on what kind of EJBs you are allowed to request?

Thanks in advance!

Marco
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaikiran Pai wrote:

Jothi Shankar Kumar wrote: Can I inject a EJB resource in a Servlet?



Yes, you can.



Does this mean that the Servlet is a managed resource of the EJB Container?
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marco Ehrentreich wrote:How does this work for Servlets which are usually reused and shared between multiple clients, too?



It works in a similar way. Injecting SFSB in servlet is not recommended.
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, Jaikiran! That makes things clearer.

Marco
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jothi,

Does this mean that the Servlet is a managed resource of the EJB Container?


I guess, otherwise DI wouldn't really work. Or do you mean anything else by "managed"?

Marco
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We cannot use the EJB annotation in a class which is not managed by the EJB container. Am I right? In this sense, can we use this annotation in a Servlet?? Is the Servlet managed by the EJB Container?
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
EJBs and Servlets are both managed by an application server even though there may be usually distinct sub-containers for EJBs and Servlets respectively. But without the application server knowing about both components DI of EJB resources of course wouldn't work here as expected. (Maybe it would compile with the JARs containing the annotation declarations in place but it surely wouldn't inject anything at runtime).

Marco
 
Looky! I'm being abducted by space aliens! Me and this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic