Access to a session bean from a request bean


I want to know how to access to my SessionBean from the java code of the request bean?

I've only done this from the JSF page with the #{...}

Thanks you !
Who is Participating?
NargzulAuthor Commented:
If JSF can access to the Session Bean without interface, we can do it too!

After many research I've find this:

(sessionInformations is my sessionBean). I've tested and it works really good, and I find this really simplier than create a new interface for each Session Bean.

(In addition, I will say, it's really much easier to use injection to access to EJB)

sessionInformations sessInf = (sessionInformations) FacesContext.getCurrentInstance().getApplication().createValueBinding("#{sessionInformations}").getValue(FacesContext.getCurrentInstance());

Open in new window

>>>I've only done this from the JSF page with the #{...}

Can you please elaborate more.
NargzulAuthor Commented:
I've only call bean in the jsf code.


<h:outputText value="#{MySessionBean.myvalueA}"/>

And now, I want to to know, to to access to this same value, but from a request Bean.

So how do we do? We just create a

MySessionBean msb = new MySessionBean(); ?

and access by this object after?

Must we do a lookup to get this session bean?
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

You will have to do la lookup to the session bean, a better idea might be to do the lookup through the request bean, and access the methods through there.
See any Java EE tutorial for the application server you're using.
It's not that hard, but the exact details are somewhat specific to the server the EJB is running on (especially the parameters to the JNDI lookup).
It also depends on the version of the EJB. The lookup for EJB 3 is quite different from those of earlier versions.

In general (abbreviated):
- do JNDI lookup
- retrieve looked-up bean
- cast to EJB interface
- call methods
NargzulAuthor Commented:
But, session/request/.... beans are a part of EJB???

Have you some code exemple? It's the only thing I need :)

I use glassfish.
If you want to lookup a session bean you need to do this -

        Properties properties = new Properties();;
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
        properties.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
        properties.put(Context.PROVIDER_URL, "localhost:1099");

        InitialContext context;

            context = new InitialContext(properties);
            TestbeanLocalInterface module = (TestbeanLocalInterface) context.lookup("Testbean/local");
        catch (NamingException e)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.