JSF HttpSession - How to access the servlet session from JSF

JSF HttpSession FAQ - How do I access the traditional HttpSession from my JSF code?

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);
}