Java EE 6 SDK |
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 |
The application is built and deployed as a WAR file.
The web module consists of:
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).
Perform the following steps to build, deploy, and run the
application:
app_dir
is the sample application base
directory: samples_install_dir/javaee6/rest/managed-beans-war
.
Change directory to app_dir.
all
target.
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
app_dir> ant
clean
Perform the following steps to build, deploy, and run the application using NetBeans IDE:
samples_install_dir/javaee6/rest/
directory, select managed-beans-war
, and click Open Project.managed-beans-war
and
select Run to build, deploy, and run the project.http://localhost:8080/managed-beans-war
.
init:Browser output
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)
If you run into a problem, refer the troubleshooting document.
Copyright © 1997-2010 Oracle and/or its affiliates. All rights reserved.