• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to access an EJB session bean outside of EAR in the same app server?  RSS feed

 
tom chansky
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new to EJB3. If I've a stateless session bean like the following:

@Local
public interface Cart {
...
}

@Stateless
public class CartBean implements Cart {
....
}

and I package this EJB bean in an EAR file, and then deploy the EAR in an application server. So, if I've a different web application also deployed in the same server, and want to access to that EJB3 bean inside that EAR file, how do I do that?

I understand that the EJB3 bean is exposed as a MBean, right? So, I should be able to access to that EJB3 bean via InitialContext.lookup(), correct? Or, should use I use something else?
 
Nitin Takale
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can access your bean by using dependency injection ... e.g. if you want to access your bean from a servlet present on the same server.. so inside servlet at class level..declare a variable of your bean type as ...

@EJB
MyBean beanRef;

public void doGet(HttpServleRequest req, HttpServletResponse res)throws...{


}

the container will automatically assign the SessionObject reference to the variable that you've declared.

See I'm guessing you are not using any deployment descriptor. So just import your package and declare ref variable.

Else you can also do the lookup and get the reference variable.

Here one point is important. If you are not using server specific deployment descriptors then you'll have to use fully qualified remote interface name while doing the lookup. Cuz with this name the bean gets registered in JNDI. Again this JNDI naming of beans is not standardized it's server specific... Glassfish V2 uses fully qualified name of interface for registering the bean in JNDI.

I hope this will solve your problem

Nitin
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!