GeeCON Prague 2014*
The moose likes EJB and other Java EE Technologies and the fly likes [Glassfish, EJB3, web client] EJB name vs. mappedName, JSP and dependency injection 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 "[Glassfish, EJB3, web client] EJB name vs. mappedName, JSP and dependency injection" Watch "[Glassfish, EJB3, web client] EJB name vs. mappedName, JSP and dependency injection" New topic
Author

[Glassfish, EJB3, web client] EJB name vs. mappedName, JSP and dependency injection

Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
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 :-)

Kind regards.
[ November 05, 2008: Message edited by: Raf Szczypiorski ]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10146
    
165

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.


[My Blog] [JavaRanch Journal]
Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
Wow, what a prompt answer, thank you very much.

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 ;-)

Cheers.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10146
    
165

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
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.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10146
    
165

Originally posted by Raf Szczypiorski:
Do I need to add anything to the web.xml file of my web client or sth?


What does your web.xml look like? If should have a reference to the web-app_2_5.xsd. Something like:

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

Do you have that?

Edited by Jaikiran: Could not enclose the above tag neither in Code block nor in Quote block.
[ November 05, 2008: Message edited by: Jaikiran Pai ]
Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
The web.xml has:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

ejb-jsr.xml has:

<ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:ejb="http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" version="3.0">

These were automatically generated by eclipse.

[ November 05, 2008: Message edited by: Raf Szczypiorski ]
[ November 05, 2008: Message edited by: Raf Szczypiorski ]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10146
    
165

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
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
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 ]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10146
    
165

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
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
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.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10146
    
165

Originally posted by Raf Szczypiorski:
Do you have experience with other application servers? How do they behave?


I keep using JBoss for my sample applications. Let me see how it behaves there.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10146
    
165

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?"


Thanks!
lalit upadheyay
Ranch Hand

Joined: Jun 20, 2005
Posts: 110
Hi,

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.

Thanks and Regards.
Lalit Upadheyay


SCJP1.4, SCWCD1.4, SCBCD5.0(working on...)
lalit upadheyay
Ranch Hand

Joined: Jun 20, 2005
Posts: 110
Hi All,


Anyone kind enough to answer above post ?


Thanks and Regards.
Lalit Upadheyay
Sam Woodard
Greenhorn

Joined: Jun 21, 2009
Posts: 5
I am having a similar problem, but I cannot even inject my ejbs into my servlet.



The commented out code works, but the injection does not. I get a null pointer.
madusanka wanniarachchi
Greenhorn

Joined: Apr 16, 2008
Posts: 4
hi sam,

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.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10146
    
165

lalit upadheyay wrote:

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).
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10146
    
165

Sam Woodard wrote:I am having a similar problem, but I cannot even inject my ejbs into my servlet.





This isn't a servlet.
John Arevalo
Greenhorn

Joined: Jul 29, 2009
Posts: 2
Maybe an old post, but

DI is not ready on Construct methods, Use an initilize method like:
H Fauche
Greenhorn

Joined: Dec 07, 2011
Posts: 1
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 ?
Andrew Moko
Ranch Hand

Joined: Dec 16, 2011
Posts: 55
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [Glassfish, EJB3, web client] EJB name vs. mappedName, JSP and dependency injection