8.1.6 Using JavaServer Faces Technology for Java Web Applications
JavaServer Faces provides new techniques and components for building user interfaces (UIs) for server-side applications. In fact, JSF is a server-side technology for developing Web applications with rich user interfaces. Before JavaServer Faces, developers who built Web applications had to rely on building HTML user interface components with Servlets or JavaServer Pages. This is mainly because HTML user interface components are the lowest common denominator that Web browsers support. One of the defects of using HTML or JSP techniques to build Web applications is that such Web applications do not have rich user interfaces compared with standalone fat clients and therefore have less functionality and/or poor usability. One of the possible solutions is to use Applets to develop rich user interfaces; however, in most cases, Web application developers do not always know whether those Applets are signed or unsigned or whether they can access the local database files. This greatly limits the implementation of Applets in Java Web database applications.
A good solution is to use JavaServer Faces technique that provides a set of rich GUI compo-nents and can be installed and run on the server side. The GUI components provided by JSF are represented by a collection of component tags. All component tags are defined and stored in the UIComponent class. A model-view-controller model is applied to the JSF technique.
The JSF technology consists of the following main components:
- JSF APIs used to represent UI components, manage state, handle events and validate input. The UI components are represented and implemented using JSF tags. The API has support for internationalization and accessibility.
- A special Servlet class, FacesServlet, that is located on the server side and works as a con-troller to handle all JSF-related events.
- JSP pages that contain rich user interface components represented by customer tags that work as views. The GUI of a JSF page is one or more JSP pages that contain JSF compo-nent tags.
- Two JSP custom tag libraries used for expressing JSF user interface components within a JSP page and wiring components to server-side objects. Page authors can easily add UI components to their pages.
- Java bean components used to work as model objects.
- An application configuration resource file, faces-config.xml, used to define the navi-gation rules between JSP pages and register Java backing beans.
- Web deployment descriptor file, web.xml, used to define the FaceServlet and its mapping.
JavaServer Faces technology is basically built based on JavaServer Pages and Servlet techniques. It uses JSP pages as the GUI and FacesServlet as the Web container. A high-level architecture of JSF is shown in Figure 8.23.
It can be seen from Figure 8.23 that a JSF Web application is composed of JSP pages represent-ing the user interface components using the JSF custom tag library and FacesServlet Web container that can be considered part of the Servlet class and takes care of the JSF-related events.
JSF defines two standard tag libraries (Core and HTML) that you have to declare in your JSP pages with the <%@taglib%> directive. The two tag libraries are:
- html _ basic.tld: A JSP custom tag library for building JSF applications that render to an HTML client.
- jsf _ core.tld: A JSP custom tag library for representing core actions independently of a particular render kit.

FIGURE 8.23 High-level architecture of JSF.
The JSF core library contains tags that do not depend on any markup language, while the JSF HTML library was designed for pages that are viewed in a Web browser. The standard prefixes of
the two tag libraries are f for the JSF Core and h for the JSF HTML. All JSF tags must be nested inside an<f:view> element. The <f:view> tag allows the JSF framework to save the state of the UI components as part of the response to a HTTP request.
To use these customer tags to represent JSF components in JSP pages, one needs to indicate them by using the following two taglib directives at the top of each JSF file:
- <%@ taglib uri=“http://java.sun.com/jsf/html” prefix=“h” %>
- <%@ taglib uri=“http://java.sun.com/jsf/core” prefix=“f” %>
The uri is used to indicate the location of the customer tag library.
JavaServer Faces pages are just regular JSP pages that use the standard JSF tag libraries or other libraries based on the JSF API. When using JSF tag components to build a JavaServer Page, a component tree or view is created in the server side memory, and this tree will be used by the JSF framework to handle requests coming from clients and send responses to the clients. Each JSF tag component is mapped to a component class defined in the UIComponent class. In fact, each tag is an instance of the mapped class in the UIComponent.
JSF utilizes a model-view-controller architecture, which means that it uses Java beans as models to stored application data, JSF GUI as the view and the Servlet as the controller.