Using JSF with AJAX

he Java Enterprise Edition 5 platform includes JavaServer Faces (JSF) technology, which provides a mature and extensible user interface component model. The design of this model makes it easy for application developers to create custom components by extending the standard components included with JSF and to reuse these components across applications. Creating a component shields the page author from the complexities of AJAX by rendering all the HTML and JavaScript code that is required to incorporate AJAX capabilities into an application. There are many ways to wrap AJAX functionality using JSF, and this document will briefly mention several strategies. You can see the detailed write up on each related startegy in the following documents:


Developers who want to include AJAX support in JSF applications are faced with many design questions. For example,

  • Should AJAX request handling be done in a separate servlet controller or should it be handled by the JSF life cycle?
  • How can existing components be AJAX-enabled without rewriting them?
  • Should JavaScript code be embedded in Java code or externalized in a separate script file?
Those questions and more are addressed in each strategy.

Developers who want to include AJAX support in JSF applications have more than one strategy to choose from. We will cover two types of startegies at this time. One point to stress is that the programming model differs between component writer and page developer. The strategies covered here can be used to add AJAX support to just a part of your application or to create re-useable JSF components. Though it is a bit of extra work to create an AJAX enabled JSF component, it makes it very easy for page authors to re-use that component.

In the first strategy, using a PhaseListener to fulfill AJAX requests in JSF components, the JSF life cycle and the custom component handles or decorates the AJAX requests. The second strategy, using a Servlet to act as a controller, introduces a Servlet to process AJAX requests to be used with the JSF components. In the second strategy, all asynchronous requests will therefore be handled outside of the JSF life cycle processing. In general, the first startegy is more common and the second startegy is more of a special case.