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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

>>>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?
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.
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

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)
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.