• 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

Having trouble injecting EJB into a servlet

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm fairly new to EJB and I'm trying to get them into my web application. I have a stateless bean that is defined like this:
MaintainItemFacade (the class interface)
MaintainItemFacadeRemote (extends above with remote)
MaintainItemFacadeBean (implements the above)

This works fine when my server is running and I access it with a standalone test client.

Now I have a separate project running on the same server, which has a servlet that I would like to access this bean's interface.

So, I've created a class InjectionTest, and immediately inside the class it says:

@EJB
private MaintainItemFacadeRemote miBean;



And then in the doGet method, it says:

PrintWriter out = response.getWriter();
out.println("You bean is "+miBean);



When I load this page, it tells me that the bean is null. What am I missing?

(I've seen similar threads that advised me to check if web.xml is running version 2.5. Yes it is.)

Russell
 
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
Which application server do you use? And is the WAR and the EJB jar separate projects? Or are they packaged in the same EAR?
 
Russell Glasser
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaikiran Pai wrote:Which application server do you use? And is the WAR and the EJB jar separate projects? Or are they packaged in the same EAR?



I was using JBoss 4.2.3. After posting, I noticed some discussions saying that this version may not handle injection properly, so I've been trying to migrate to a newly installed 5.0.1. Just have to remind myself how all the configuration works.

They are in separate packages, which I created through Eclipse. I have made an EAR. Do I need to?
 
Russell Glasser
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately, I haven't been able to make further progress on this problem. I tried packaging both my EJB project and my dynamic web project into an Enterprise Application, and I'm now loading that EAR on the server instead of my original separate projects.

Not only can I still not access the bean from within a servlet, but after I add it to the archive, even my test clients no longer work. It fails on the JNDI lookup (message: "javax.naming.NameNotFoundException: remote not bound" on the step where I perform jndiContext.lookup.

If I take it out of the EAR and load the EJB project directly into the server, the client works fine. Does something change with the naming or the context when I make an EAR?
 
Russell Glasser
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Russell Glasser wrote:Unfortunately, I haven't been able to make further progress on this problem. I tried packaging both my EJB project and my dynamic web project into an Enterprise Application, and I'm now loading that EAR on the server instead of my original separate projects.

Not only can I still not access the bean from within a servlet, but after I add it to the archive, even my test clients no longer work. It fails on the JNDI lookup (message: "javax.naming.NameNotFoundException: remote not bound" on the step where I perform jndiContext.lookup.

If I take it out of the EAR and load the EJB project directly into the server, the client works fine. Does something change with the naming or the context when I make an EAR?



Okay, well I fixed that problem, I had to include the name of the EAR as part of the lookup. So now the name is "KolSalesApp/MaintainItemFacade/remote" and the client works.

To my dismay, this did not fix the original problem. I called my web client which includes this declaration:

@EJB(name="KolSalesApp/MaintainItemFacade/remote")
private MaintainItemFacadeRemote miBean;

When I attempt to inspect miBean, it's still null. Maybe there is an extra step required?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have a "jboss-web.xml" file in the war? If not, then you should to create this file and declare the ejb reference.

Check this link for more info: https://jira.jboss.org/jira/browse/EJBTHREE-914
 
Russell Glasser
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fixed the problem. For the benefit of future surfers finding this thread there were two problems:

1. Upgrading to JBoss 5.0 was a big help -- 4.x does not really handle injection elegantly.
2. Changed the bean definition from remote to local, since it is being used within one EAR.

Now I'm just trying to figure out how to use PersistentContext so that the servlet can actually update the data, but that's a different problem.
 
girl power ... turns out to be about a hundred watts. But they seriuosly don't like being connected to the grid. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic