Solved

Access to a session bean from a request bean

Posted on 2009-06-30
7
604 Views
Last Modified: 2012-05-07
Hi!

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 !
0
Comment
Question by:Nargzul
7 Comments
 
LVL 7

Expert Comment

by:gudii9
ID: 24751106
>>>I've only done this from the JSF page with the #{...}

Can you please elaborate more.
0
 
LVL 1

Author Comment

by:Nargzul
ID: 24751239
I've only call bean in the jsf code.

like:

<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?
0
 
LVL 20

Expert Comment

by:a_b
ID: 24751458
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.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 6

Expert Comment

by:jwenting
ID: 24751773
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
0
 
LVL 1

Author Comment

by:Nargzul
ID: 24753456
But, session/request/.... beans are a part of EJB???

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

I use glassfish.
0
 
LVL 20

Expert Comment

by:a_b
ID: 24753536
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;

        try
        {
            context = new InitialContext(properties);
            TestbeanLocalInterface module = (TestbeanLocalInterface) context.lookup("Testbean/local");
           
        }
        catch (NamingException e)
        {
            e.printStackTrace();
        }
0
 
LVL 1

Accepted Solution

by:
Nargzul earned 0 total points
ID: 24768052
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

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array11 challenge 16 51
count11 challenge 6 47
JAVA part two 5 40
advertisement module in core php 4 87
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now