Document Information


Part I Introduction

1.  Overview

2.  Using the Tutorial Examples

Part II The Web Tier

3.  Getting Started with Web Applications

4.  JavaServer Faces Technology

What Is a JavaServer Faces Application?

JavaServer Faces Technology Benefits

Further Information about JavaServer Faces Technology

5.  Introduction to Facelets

6.  Expression Language

7.  Using JavaServer Faces Technology in Web Pages

8.  Using Converters, Listeners, and Validators

9.  Developing with JavaServer Faces Technology

10.  JavaServer Faces Technology: Advanced Concepts

11.  Using Ajax with JavaServer Faces Technology

12.  Composite Components: Advanced Topics and Example

13.  Creating Custom UI Components and Other Custom Objects

14.  Configuring JavaServer Faces Applications

15.  Java Servlet Technology

16.  Uploading Files with Java Servlet Technology

17.  Internationalizing and Localizing Web Applications

Part III Web Services

18.  Introduction to Web Services

19.  Building Web Services with JAX-WS

20.  Building RESTful Web Services with JAX-RS

21.  JAX-RS: Advanced Topics and Example

Part IV Enterprise Beans

22.  Enterprise Beans

23.  Getting Started with Enterprise Beans

24.  Running the Enterprise Bean Examples

25.  A Message-Driven Bean Example

26.  Using the Embedded Enterprise Bean Container

27.  Using Asynchronous Method Invocation in Session Beans

Part V Contexts and Dependency Injection for the Java EE Platform

28.  Introduction to Contexts and Dependency Injection for the Java EE Platform

29.  Running the Basic Contexts and Dependency Injection Examples

30.  Contexts and Dependency Injection for the Java EE Platform: Advanced Topics

31.  Running the Advanced Contexts and Dependency Injection Examples

Part VI Persistence

32.  Introduction to the Java Persistence API

33.  Running the Persistence Examples

34.  The Java Persistence Query Language

35.  Using the Criteria API to Create Queries

36.  Creating and Using String-Based Criteria Queries

37.  Controlling Concurrent Access to Entity Data with Locking

38.  Using a Second-Level Cache with Java Persistence API Applications

Part VII Security

39.  Introduction to Security in the Java EE Platform

40.  Getting Started Securing Web Applications

41.  Getting Started Securing Enterprise Applications

42.  Java EE Security: Advanced Topics

Part VIII Java EE Supporting Technologies

43.  Introduction to Java EE Supporting Technologies

44.  Transactions

45.  Resources and Resource Adapters

46.  The Resource Adapter Example

47.  Java Message Service Concepts

48.  Java Message Service Examples

49.  Bean Validation: Advanced Topics

50.  Using Java EE Interceptors

Part IX Case Studies

51.  Duke's Bookstore Case Study Example

52.  Duke's Tutoring Case Study Example

53.  Duke's Forest Case Study Example



Creating a Simple JavaServer Faces Application

JavaServer Faces technology provides an easy and user-friendly process for creating web applications. Developing a simple JavaServer Faces application typically requires the following tasks:

  • Developing managed beans

  • Creating web pages using component tags

  • Mapping the javax.faces.webapp.FacesServlet instance

This section describes those tasks through the process of creating a simple JavaServer Faces Facelets application.

The example is a Hello application that includes a managed bean and a web page. When accessed by a client, the web page prints out a Hello World message. The example application is located in the tut-install/examples/web/hello/ directory. The tasks involved in developing this application can be examined by looking at the application components in detail.

Developing the Managed Bean

As mentioned earlier in this chapter, a managed bean is a lightweight container-managed object. Components in a page are associated with managed beans that provide application logic. The example managed bean,, contains the following code:

package hello;

import javax.faces.bean.ManagedBean;

public class Hello {

    final String world = "Hello World!";

    public String getworld() {
        return world;

The example managed bean sets the value of the variable world with the string "Hello World!". The @ManagedBean annotation registers the managed bean as a resource with the JavaServer Faces implementation. For more information on managed beans and annotations, see Chapter 9, Developing with JavaServer Faces Technology.

Creating the Web Page

In a typical Facelets application, web pages are created in XHTML. The example web page, beanhello.xhtml, is a simple XHTML page. It has the following content:

<html lang="en"
        <title>Facelets Hello World</title>

A Facelets XHTML web page can also contain several other elements, which are covered later in this tutorial.

The web page connects to the managed bean through the Expression Language (EL) value expression #{}, which retrieves the value of the world property from the managed bean Hello. Note the use of hello to reference the managed bean Hello. If no name is specified in the @ManagedBean annotation, the managed bean is always accessed with the first letter of the class name in lowercase.

For more information on using EL expressions, see Chapter 6, Expression Language. For more information about Facelets technology, see Chapter 5, Introduction to Facelets. For more information about the JavaServer Faces programming model and building web pages using JavaServer Faces technology, see Chapter 7, Using JavaServer Faces Technology in Web Pages.

Mapping the FacesServlet Instance

The final task requires mapping the FacesServlet, which is done through the web deployment descriptor (web.xml). A typical mapping of FacesServlet is as follows:

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

The preceding file segment represents part of a typical JavaServer Faces web deployment descriptor. The web deployment descriptor can also contain other content relevant to a JavaServer Faces application configuration, but that information is not covered here.

Mapping the FacesServlet is automatically done for you if you are using an IDE such as NetBeans IDE.

The Lifecycle of the hello Application

Every web application has a lifecycle. Common tasks, such as handling incoming requests, decoding parameters, modifying and saving state, and rendering web pages to the browser, are all performed during a web application lifecycle. Some web application frameworks hide the details of the lifecycle from you, whereas others require you to manage them manually.

By default, JavaServer Faces automatically handles most of the lifecycle actions for you. However, it also exposes the various stages of the request lifecycle, so that you can modify or perform different actions if your application requirements warrant it.

It is not necessary for the beginning user to understand the lifecycle of a JavaServer Faces application, but the information can be useful for creating more complex applications.

The lifecycle of a JavaServer Faces application starts and ends with the following activity: The client makes a request for the web page, and the server responds with the page. The lifecycle consists of two main phases: execute and render.

During the execute phase, several actions can take place:

  • The application view is built or restored.

  • The request parameter values are applied.

  • Conversions and validations are performed for component values.

  • Managed beans are updated with component values.

  • Application logic is invoked.

For a first (initial) request, only the view is built. For subsequent (postback) requests, some or all of the other actions can take place.

In the render phase, the requested view is rendered as a response to the client. Rendering is typically the process of generating output, such as HTML or XHTML, that can be read by the client, usually a browser.

The following short description of the example JavaServer Faces application passing through its lifecycle summarizes the activity that takes place behind the scenes.

The hello example application goes through the following stages when it is deployed on the GlassFish Server.

  1. When the hello application is built and deployed on the GlassFish Server, the application is in an uninitiated state.

  2. When a client makes an initial request for the beanhello.xhtml web page, the hello Facelets application is compiled.

  3. The compiled Facelets application is executed, and a new component tree is constructed for the hello application and is placed in a javax.faces.context.FacesContext.

  4. The component tree is populated with the component and the managed bean property associated with it, represented by the EL expression

  5. A new view is built, based on the component tree.

  6. The view is rendered to the requesting client as a response.

  7. The component tree is destroyed automatically.

  8. On subsequent (postback) requests, the component tree is rebuilt, and the saved state is applied.

For more detailed information on the JavaServer Faces lifecycle, see Chapter 10, JavaServer Faces Technology: Advanced Concepts.

Running the hello Application

You can use either NetBeans IDE or Ant to build, package, deploy, and run the hello example.

To Run the hello Application in NetBeans IDE

  1. From the File menu, choose Open Project.
  2. In the Open Project dialog box, navigate to:
  3. Select the hello folder.
  4. Select the Open as Main Project check box.
  5. Click Open Project.
  6. In the Projects tab, right-click the hello project and select Run.

    This step compiles, assembles, and deploys the application and then brings up a web browser window displaying the following URL:


    The output looks like this:

    Hello World!

To Run the hello Example Using Ant

  1. In a terminal window, go to:
  2. Type the following command:

    This target builds the WAR file and copies it to the tut-install/examples/web/hello/dist/ directory.

  3. Type ant deploy.
  4. In a web browser, type the following URL:

    The output looks like this:

    Hello World!