my dog learned polymorphism*
The moose likes EJB and other Java EE Technologies and the fly likes Having trouble injecting EJB into a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Having trouble injecting EJB into a servlet" Watch "Having trouble injecting EJB into a servlet" New topic
Author

Having trouble injecting EJB into a servlet

Russell Glasser
Greenhorn

Joined: May 22, 2009
Posts: 5
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
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9930
    
159

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


[My Blog] [JavaRanch Journal]
Russell Glasser
Greenhorn

Joined: May 22, 2009
Posts: 5
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

Joined: May 22, 2009
Posts: 5
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

Joined: May 22, 2009
Posts: 5
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?
Rhold Aquino
Greenhorn

Joined: Jun 23, 2008
Posts: 5
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


Harold '94<br />==================================<br />SCEA 5, SCBCD 1.3/5, SCWCD 1.4, SCJP 1.2, SCJA<br /> <br />Certified JBOSS 4 Administrator
Russell Glasser
Greenhorn

Joined: May 22, 2009
Posts: 5
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Having trouble injecting EJB into a servlet
 
Similar Threads
EJB and Database
EJB DI
EJB Structure used in Client-Server Program?
Proxy stub generation
how to access one ejb with local interface from different application deployed in same server.