File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes access one bean from another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "access one bean from another" Watch "access one bean from another" New topic

access one bean from another

Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
I've got 2 beans that don't know about each other (and I do not believe they are EJBs). I need one to access an attribute of another, and I'm thinking there's a way to get ahold of the SessionContext and use that to access the other bean and eventually get a value stored in that bean. I know that when I'm in a servlet's doPost method, I can call something like this:

So when I'm in another bean and don't have access to that HttpServletRequest object, how can I get ahold of that UserAcctBean? Or how can I get ahold of the session? Or do I get ahold of a SessionContext and do something with that?
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
I posted a link to this message in the servlet forum in case more readers there would know the answer to this.
Ankur Srivastava
Ranch Hand

Joined: May 11, 2004
Posts: 62
If it is an entity bean then it would have been very easy. You can get the refference to the remote object using a context lookup and access any field of that Entity Bean but as you mention that you beans are not EJBs then probably this approach should work.
Extend the one bean class by another ans set the scope of bean where ever you are refferencing it as session.
raphael Bereh
Ranch Hand

Joined: Jan 15, 2002
Posts: 79
I am not sure I understand you issue, nut here is a typical remote client code to access an EJB :
Context context = new InitialContext();
BeanHome home = (BeanHome)javax.rmi.PortableRemoteObject.narrow(context.lookup(BEAN_JNDI_NAME_FOR_HOME),BeanHome.class);
BeanComponent_Interface bean = home.creat<METHOD>(...);
int x = bean .method_cal(); // method call
Hope this, helps,

I agree. Here's the link:
subject: access one bean from another
It's not a secret anymore!