JSF HttpSession – How to access the servlet session from JSF

Posted: November 15, 2011 in Random Posts
Tags: , , , , , , , ,

I’ve been working with JavaServer Faces (JSF) a lot lately, and one thing I was curious about was how I can get back to the old-fashioned Java session (HttpSession) if and when I need to, while also wondering what was in the session.

Here’s some sample Java code that shows how you can access the traditional session from your own JSF code, and also prints whatever is in that session. As you can see, it takes essentially one or two lines of code to get to the HttpSession reference:

FacesContext facesContext = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements())
{
String attr = (String)e.nextElement();
System.err.println(” attr = “+ attr);
Object value = session.getValue(attr);
System.err.println(” value = “+ value);
}

Advertisements
Comments
  1. […] ProgrammesHit Counter – Net Analytics Servicesipad tabletApache Tomcat/7.0.6UpcomingDiscs.comJSF HttpSession – How to access the servlet session from JSFUpcomingDiscs.comJSF HttpSession – How to access the servlet session from […]

    Like

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s