Accessing Context Data in Beans using JSF

By: Henry Viewed: 192 times  Printer Friendly Format    


Although JSF makes it easy for you to connect the view with the model without writing any code, at times you may need direct access to the request data or other data of your web application. 

JSF provides access to the request data and other data through the FacesContext object. As the Javadoc states, “FacesContext contains all of the per-request state information related to the processing of a single JavaServer Faces request, and the rendering of the corresponding response . . . . A FacesContext instance is associated with a particular request at the beginning of request processing.” Because a FacesContext instance, and the objects it contains, are associated with the thread processing a particular request, any references to the FacesContext instance or its contained objects must not be passed to another thread or stored for subsequent requests.

You can access the FacesContext object like this:

FacesContext context = FacesContext.getCurrentInstance();

After you have a reference to the FacesContext for the request, you can access all the request and application data through the ExternalContext object. The reference to the ExternalContext object is obtained through the call to the getExternalContext() method. Table below shows the methods of ExternalContext that return collections of request and application data. You can use these methods to access request data, session data, request header data, cookie data, and other sets of data in the application.



Most likely, if you are accessing request or application data in a managed bean, you will be accessing request data or session data. As you can see in Table, you access request data through the getRequestParameterMap() or getRequestParameterValuesMap() method. You can access session data through the getSessionMap() method.



Most Viewed Articles (in JSF )

Latest Articles (in JSF)

Comment on this tutorial