Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

session not available in a jsp file?

 
reubin haz
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JSF project. In the back beaning classes, I use the following to access session:

HttpSession session =(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);

But FacesContext.getCurrentInstance() is throwing null pointer exception in a Java class that is not getting used by any jsp, even though I registered this class in face-config.xml as <managed-bean>.

Why would I get this error? How should I write a Java class so that FacesContext is instanticated before I use it?

Thanks.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18098
50
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FacesContext is instantiated and destroyed on each JSF HTTP request. It doesn't linger between requests, and it is only created by the Faces Servlet, so if an Http URL request doesn't get routed through the Faces Servlet, you won't have a FacesContext. That specifically means non-faces servlet requests and page requests made to stand-alone JSPs that aren't JSF JSPs.

Usually, if you're attempting to explicitly access a session inside a JSF managed bean, you're doing something wrong. That's because the JSF bean management framework should probably be injecting what you need instead of coding the bean to be explicitly dependent on running inside JSF.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic