• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accessing JSF sesson bean from a servlet

 
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have this managed session bean defined in my faces-config.xml file:

<managed-bean>
<managed-bean-name>ConfigLoginBean</managed-bean-name>
<managed-bean-class>com.comcast.npsconfig.login.ConfigLoginBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

How do I access this bean from my servlet? I've noticed invoking FacesContext.getInstances() returns null. This is what I currently have, which fails because of the NullPointerException.

private void doRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(false);
...

// Get the config login bean.
FacesContext ctx = FacesContext.getCurrentInstance();
Application app = ctx.getApplication();
ConfigLoginBean configLoginBean = (ConfigLoginBean)app.getVariableResolver().resolveVariable(ctx, "ConfigLoginBean");
configLoginBean.finishLogin(uc);
...
}

Thanks for your help, - Dave
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can try below code
HttpSession session = request.getSession(false);
ConfigLoginBean configLoginBean = (ConfigLoginBean) session.getAttribute("ConfigLoginBean");
 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I tried this and even on JSF pages, this returns null. - Dave
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that this can solve your problem.

And the answer to your last post is that the Servlet is out of the FacesContext so it will be always null and the JSF Beans will not be out of the FacesContext.
[ July 11, 2008: Message edited by: Andres Quinones ]
 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was it! Thanks for the help, - Dave
 
Dinner will be steamed monkey heads with a side of tiny ads.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic