File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes session not available in a jsp file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "session not available in a jsp file?" Watch "session not available in a jsp file?" New topic

session not available in a jsp file?

reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
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?


Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

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.

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: session not available in a jsp file?
It's not a secret anymore!