• 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

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

 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
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
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.
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
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
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
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
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

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
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
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
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
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
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
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
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
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

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

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!
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
lalit upadheyay
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,


Anyone kind enough to answer above post ?


Thanks and Regards.
Lalit Upadheyay
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
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

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

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





This isn't a servlet.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe an old post, but

DI is not ready on Construct methods, Use an initilize method like:
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic