session handling in JSF 1.2

How do we handle session variables (add/fetch) in JSF 1.2?

A scenario: Consider a login screen where the user logs in successfully, the user model is stored in session. The user modes contains the user role. Next time onwards, for every user action, check the user role from the user model and display the form accordingly. In such a case how to add the user odel in session and how to etrieve it every time from the session?

Previously I have worked in Struts 1.2 where in the execute method, we have a request e=which is used to get the session and access the session variables also. But I am not sure how to achieve th same in JSF 1.2.

Is the only way achieveable is to add the managed bean in the session scope in the faces-config.xml file?

Please help me out with the session handling concepts in JSF 1.2.


The session scope is programmatically available by ExternalContext#getSessionMap() which delegates under the covers to HttpSession#get/setAttribute().

Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
// ...

You can of course also just put a managed bean in the session scope. It's accessible from other managed beans by <managed-property> (or just a traversal of the session map using the managed bean name as map key).

I think you can use Java EE filters for this mechanism.

Filters are controlled by Servlet Container and runs first on an action depending on your web.xml order.

Add a servlet filter to your project.

public class YourFilter implements Filter {

public static final String USER = "USER_SESSION_KEY";

public void doFilter(ServletRequest req, ServletResponse response, FilterChain filterChain)

   HttpServletRequest request = (HttpServletRequest) req;
   HttpSession session = request.getSession(true);
       String servletpath = request.getServletPath();
if(!servletpath.contains("login.xhtml")) //exclude your login page and other pages required to pass this filter.
     if (session.getAttribute(USER) != null)
    //Control your authentication and roles.
    //There is no user in the session.

    filterChain.doFilter(request, response);

Add your filter to your web.xml

    <servlet-name>Faces Servlet</servlet-name>

Secondly, put your User class to the session inside a JSF action.

public void userAction()

 User user = new User();
   //Build your User Class
  HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    request.getSession(false).setAttribute("USER", user);


P.S. : User class is a user defined POJO class. you should implement it according to your needs.

public class User
    private String username;
       //Other properties and getter setter methods required.


If you want to implement this mechanism inside JSF context. You can build the same logic by implementing JSF phase listeners.

Need Your Help

C++ and friendship of nested private classes

c++ friend nested-class

I have 2 classes like the dummy one below

Tool to reformat VB.Net code - specifically line breaks reformat

Are there any tools available for automatically formatting code - specifically for adding line breaks at a predefined line length? I'm working with a lot of code with long lines (thousands of

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.