This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
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?
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
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 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!
Sorry, i can't seem to understand this question Can you please elaborate?
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...
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.
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?
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).