I have an ear application that consists of an ejb.jar and a web application (war). The ejb.jar contains all my EJBs (beans and interfaces) and my war contains REST web services. I want to access the EJBs from the war module, is that possible? Injecting the EJBs doesn't work, I get null pointer exception.
I know that this has been asked many times but I can't seem to get this working...
I am using Glassfish v2.1.1 (I know I should upgrade, but right now it is difficult...)
Here is the code fragment that returns nullPointerException: (I know I am not supposed to validate a user through GET, but this is just an example code I am trying just to check if I can access the ejb)
@Path("/user")
public class UserWS
{
@EJB
SB_usrRemote sb_usrRemote;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String checkLoginAttempt(String username, String password)
{
return sb_usrRemote.checkLoginAttempt(username, password).toString();
}
}
I didn't mention that this code only works when I include the ejb.jar in the ear application's lib folder where the war module can "see" it. Is there any other way I can access the EJB classes? Also I am using Intellij, the application is a java EE Application that includes a web module (for the war).
EDIT
I couldn't avoid the glassfish upgrade from v2 to v4. It was the best decision I made as all my problems went away. All the ejb injection in my code worked like a charm!
Thank you both for your suggestions! I am accepting hugh 's answer because JNDI probably would have worked, but upgrading glassfish seemed like a more appropriate solution in the long run.
EDIT 2
I finally managed to do a proper ejb injection using this code:
@Path("/user")
public class UserWS
{
@EJB (lookup = "java:global/<ear-name>/<ejb-jar-name>/SB_usrRemote")
SB_usrRemote sb_usrRemote;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String checkLoginAttempt(String username, String password)
{
return sb_usrRemote.checkLoginAttempt(username, password).toString();
}
}
I found the proper lookup path in the glassfish log when I deployed my ear file.