Java EE 6 SDK 

Samples Main Page

The Managed Beans Application

This sample demonstrates the Java EE 6 feature of managed beans. JAX-RS root resource classes are annotated with @ManagedBean, which means EE-related resources may be injected into instances of those classes.

If configured properly, page http://localhost:8080/managed-beans-war will be opened after invoking the run target.

The mapping of the URI path space is presented in the following table:

URI path Resource class HTTP method Description
/managed-beans-war --- GET index page
/managed-beans-war/app/managedbean/per-request ManagedBeanPerRequestResource GET Returns injected value and increments it
/managed-beans-war/app/managedbean/singleton ManagedBeanSingletonResource GET Returns injected value and increments it

Application Structure

The application is built and deployed as a WAR file.

Web Module

The web module consists of:

The starting point of the application is the index.html page. The application consists only from two resources and main page provides links to them. Two EE-related artifacts are injected into each root resource class instance. The first is a resource constant defined in the web.xml:
<env-entry>
    <env-entry-name>injectedResource</env-entry-name>
    <env-entry-type>java.lang.Integer</env-entry-type>
    <env-entry-value>1</env-entry-value>
</env-entry>
The second is a stateless session EJB (see class enterprise.managedbeans.resources.StatelessSessionBean). The output is message provided by injected StatelessSessionBean instance concatenated with injected integer value from web.xml. You can easily test if singleton is really singleton by hitting that resource class more than once - integer value should increment by one every time you hit it. On the other hand, per-request resource will always return "message 1" (the value is incremented too, but same per-request instance wouldn't be hit twice).

Building, Deploying, and Running the Application

Perform the following steps to build, deploy, and run the application:

  1. Setup your build environment and configure the application server with which the build system has to work by following the common build instructions.
  2. app_dir is the sample application base directory: samples_install_dir/javaee6/rest/managed-beans-war.
  3. Change directory to app_dir.
  4. Build, deploy, and run the sample application using the all target.
  5. app_dir> ant all

    You can replace the ant all command with the following set of commands:

    app_dir> ant default compiles and packages the application

    app_dir> ant deploy deploys it to application server

  6. Use the target clean to undeploy the sample application and to remove the temporary directories like build and dist.

    app_dir> ant clean

Building, Deploying, and Running the Application in NetBeans IDE

Perform the following steps to build, deploy, and run the application using NetBeans IDE:

  1. Refer to the common build instructions for setting up NetBeans IDE and Java EE 6 SDK.
  2. In the NetBeans IDE, choose File → Open Project (Ctrl-Shift-O), navigate to the samples_install_dir/javaee6/rest/ directory, select managed-beans-war, and click Open Project.
  3. In the Projects tab, right click managed-beans-war and select Run to build, deploy, and run the project.
    If configured properly, the IDE opens the browser with the following URL: http://localhost:8080/managed-beans-war.
    Sample output is as follows:
    NetBeans IDE output
    init:
    deps-module-jar:
    deps-ear-jar:
    deps-jar:
    library-inclusion-in-archive:
    library-inclusion-in-manifest:
    compile:
    compile-jsps:
    Starting GlassFish v3 Prelude
    GlassFish v3 Prelude is running.
    Incrementally deploying managed-beans-war
    Completed incremental distribution of managed-beans-war
    run-deploy:
    Browsing: http://localhost:8080/managed-beans-war/
    run-display-browser:
    run:
    BUILD SUCCESSFUL (total time: 11 seconds)
    Browser output

    ManagedBeans sample

    Per-request Managed Bean

    Singleton Managed Bean

Troubleshooting

If you run into a problem, refer the troubleshooting document.

 

Copyright © 1997-2010 Oracle and/or its affiliates. All rights reserved.