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

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

The encoder Example: Using Alternatives

The Coder Interface and Implementations

The encoder Facelets Page and Managed Bean

Running the encoder Example

To Build, Package, and Deploy the encoder Example Using NetBeans IDE

To Run the encoder Example Using NetBeans IDE

To Build, Package, and Deploy the encoder Example Using Ant

To Run the encoder Example Using Ant

The producerfields Example: Using Producer Fields to Generate Resources

The Producer Field for the producerfields Example

The producerfields Entity and Session Bean

The producerfields Facelets Pages and Managed Bean

Running the producerfields Example

To Build, Package, and Deploy the producerfields Example Using NetBeans IDE

To Build, Package, and Deploy the producerfields Example Using Ant

To Run the producerfields Example

The billpayment Example: Using Events and Interceptors

The PaymentEvent Event Class

The PaymentHandler Event Listener

The billpayment Facelets Pages and Managed Bean

The LoggedInterceptor Interceptor Class

Running the billpayment Example

To Build, Package, and Deploy the billpayment Example Using NetBeans IDE

To Build, Package, and Deploy the billpayment Example Using Ant

To Run the billpayment Example

The decorators Example: Decorating a Bean

Components of the decorators Example

Running the decorators Example

To Build, Package, and Deploy the decorators Example Using NetBeans IDE

To Build, Package, and Deploy the decorators Example Using Ant

To Run the decorators Example

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



The producermethods Example: Using a Producer Method To Choose a Bean Implementation

The producermethods example shows how to use a producer method to choose between two beans at runtime, as described in Using Producer Methods, Producer Fields, and Disposer Methods in CDI Applications. It is very similar to the encoder example described in The encoder Example: Using Alternatives. The example includes the same interface and two implementations of it, a managed bean, a Facelets page, and configuration files. It also contains a qualifier type. When you run it, you do not need to edit the beans.xml file and redeploy the application to change its behavior.

Components of the producermethods Example

The components of producermethods are very much like those for encoder, with some significant differences.

Neither implementation of the Coder bean is annotated @Alternative, and the beans.xml file does not contain an alternatives element.

The Facelets page and the managed bean, CoderBean, have an additional property, coderType, that allows the user to specify at runtime which implementation to use. In addition, the managed bean has a producer method that selects the implementation using a qualifier type, @Chosen.

The bean declares two constants that specify whether the coder type is the test implementation or the implementation that actually shifts letters:

    private final static int TEST = 1;
    private final static int SHIFT = 2;
    private int coderType = SHIFT; // default value

The producer method, annotated with @Produces and @Chosen as well as @RequestScoped (so that it lasts only for the duration of a single request and response), takes both implementations as arguments, then returns one or the other, based on the coderType supplied by the user.

    public Coder getCoder(@New TestCoderImpl tci,
            @New CoderImpl ci) {

        switch (coderType) {
            case TEST:
                return tci;
            case SHIFT:
                return ci;
                return null;

Finally, the managed bean injects the chosen implementation, specifying the same qualifier as that returned by the producer method to resolve ambiguities:

    Coder coder;

The Facelets page contains modified instructions and a pair of radio buttons whose selected value is assigned to the property coderBean.coderType:

    <h2>String Encoder</h2>
        <p>Select Test or Shift, type a string and an integer, then click
        <p>If you select Test, the TestCoderImpl bean will display the
            argument values.</p>
        <p>If you select Shift, the CoderImpl bean will return a string that
            shifts the letters in the original string by the value you specify.
            The value must be between 0 and 26.</p>
        <h:form id="encodeit">
            <h:selectOneRadio id="coderType"
                    itemLabel="Shift Letters"/>

Running the producermethods Example

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

To Build, Package, and Deploy the producermethods Example Using NetBeans IDE

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

To Build, Package, and Deploy the producermethods Example Using Ant

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

    This command calls the default target, which builds and packages the application into a WAR file, producermethods.war, located in the dist directory.

  3. Type the following command:
    ant deploy

To Run the producermethods Example

  1. In a web browser, type the following URL:

    The String Encoder page opens.

  2. Select either the Test or Shift Letters radio button, type a string and the number of letters to shift by, then click Encode.

    Depending on your selection, the Result line displays either the encoded string or the input values you specified.