Hi everyone. I am learning EJB3 now, and I created (in Eclipse Ganymede with WTP2.0 and Glassfish plugin) an EJB3 project, in which I have a simple bean. The bean is stateless, and I named it "greeter":
Then, I created a web module as a separate project, added the EJB module as a dependency (so that the code compiles - I am using a Greeter cast and reference), and invoke the bean in a servelt like this:
After deploying both projects separately (EJB first, Web second), and invoking the servlet, I got this exception:
So, I changed the @Stateless annotation to @Stateless(name = "greeter", mappedName = "greeter"), and this time the invocation worked fine, my bean finally greeted me!
So, I would like to ask you, what is the difference between "name" and "mappedName", and what are the defaults if I don't specify them with the @Stateless annotation?
I know I can simply use dependency injection, but this is another test to come :-)
The "name" is to identify the bean whereas the "mappedName" is what will be used for binding the bean in the JNDI. This documentation has more details.
Now I would have another question, about dependency injection (I knew that the phrase "simply use dependency injection" from my first post would hit me back). I deleted both "name" and mappedName" from the @Stateless annotation, so that the defaults are used. My servlet (it's astually a JSP) has this code:
After deployment, on invocation, I get a NullPointerException. Could you tell me what is going wring here? In all the tutorials this is presented as simple as this, this doesn't woro for me somehow. Well, the beginning is always the toughest ;-)
Looks fine to me. Do you see any exceptions/logs in the glassfish logs? Also, if you combine the EJB jar and WAR into an EAR, does it work?
Raf Szczypiorski
Ranch Hand
Joined: Aug 21, 2008
Posts: 383
posted
0
Hi. I created an EAR project in Eclipse, run it within Eclipse - same exception. I then exported the project to an ear file, deployed it in a standalone glassfish, same problem. Do I need to add anything to the web.xml file of my web client or sth? Thanks.
Okay, let's try this in a different way. That will help us understand where the issue is.
Instead of injecting the bean through the JSP (which ultimately gets converted to a servlet), try injecting the EJB into a servlet directly:
See if this works.
Raf Szczypiorski
Ranch Hand
Joined: Aug 21, 2008
Posts: 383
posted
0
Hi. I had the same idea you had, and guess what - it works for a plain old servlet. Does this mean JSP cannot be injected? Is there any document which says which components can use injection? I know that plain classes, like model classes and so on, cannot, but I would think a JSP could, as in reality it is a servlet. Thanks.
Raf Szczypiorski
Ranch Hand
Joined: Aug 21, 2008
Posts: 383
posted
0
I checked in servlet-2_5-mrel2-spec.pdf and there is a table on page 155:
No mention of JSP directly
[ November 05, 2008: Message edited by: Raf Szczypiorski ] [ November 05, 2008: Message edited by: Raf Szczypiorski ]
I don't have experience with Glassfish, but i think it should have worked with JSPs too. After all the JSPs get converted to a servlets (which do implement the javax.servlet.Servlet interface).
Raf Szczypiorski
Ranch Hand
Joined: Aug 21, 2008
Posts: 383
posted
0
I explicitly added this:
to my web.xml, and this time it worked for my jsp. A little weird one has to specify the jsp explicitly in web.xml for injection to work. Do you have experience with other application servers? How do they behave?
Thank you so much for your interest in this.
Raf Szczypiorski
Ranch Hand
Joined: Aug 21, 2008
Posts: 383
posted
0
Final say on this has google and sun :-). Anyone interested in this and having similar problems, please take a look here: http://java.sun.com/developer/technicalArticles/J2EE/injection/, and read the section: "What Components Can Accept Resource Injections?" Cheers.
Originally posted by Raf Szczypiorski: Final say on this has google and sun :-). Anyone interested in this and having similar problems, please take a look here: http://java.sun.com/developer/technicalArticles/J2EE/injection/, and read the section: "What Components Can Accept Resource Injections?"
Can some please explain why we should use name and mappedName attribute of @Stateless annotation? From this post I conclude that mappedName provides us a way to specify the JNDI lookup key.
I know that SLSB bean isntances are pooled and sharable. Does a JNDI specify some way to refer to an Object pool as we specify same JNDI key to get reference to sharable object isntances ?
Now assume that we have an EJB implementing both the local and remote biz interfaces. How will we get reference to remote interface and local interface of this EJB ? Will there be any difference in the JNDI lookup key ?
Sorry, if some questions sound stupid as I have just started learning EJB.
the reason is this class is not a servlet. the DI supports servlets and Main classes of application client modules only(according to my knowledge).
the commented code worked because it is a JNDI lookup and it supports anywhere. the DI and lookup is totaly different.
Now assume that we have an EJB implementing both the local and remote biz interfaces. How will we get reference to remote interface and local interface of this EJB ? Will there be any difference in the JNDI lookup key ?
Lalit, you are right - the local and remote business interface(s) will have different JNDI name (lookup key).
I'm facing the same problem.
My EJB injection works in a Bean if it's behind a servlet but not behind a jsp page.
What's the difference between a servlet and a jsp generated servlet ?
It would help if you threw in some code on how you injecting the bean in a jsp page. I can see a bean being injected on a servlet (either through @EJB or a manual jndi lookup) but not on a jsp page.
In this case, i would have a managed bean that does all lookups/ bean injections. And have the jsp communicate with the managed bean through the EL expressions.