Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing JSF sesson bean from a servlet  RSS feed

 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • 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
 
Venkat Sadasivam
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks. I tried this and even on JSF pages, this returns null. - Dave
 
Andres Quinones
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
That was it! Thanks for the help, - Dave
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!