Solved

Access to a session bean from a request bean

Posted on 2009-06-30
7
610 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

740 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