Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

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
 
Hey cool! They got a blimp! But I have a tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic