Java EE 6 SDK

Samples Main Page

# A Simple WAR-based EJB 3.1 Application

This is a simple EJB 3.1 application that shows the use of WAR-based packaging , the no-interface Local view, Singleton session beans, and startup/shutdown callbacks.
Overview
The application is composed of a Servlet, a Singleton session bean, and a Stateless session bean. Each session bean exposes a no-interface view. The Singleton defines a callback that is called by the container during application initialization. Both the Servlet and Stateless bean use the Singleton to access common application configuration info. When the Servlet is accessed, it invokes the session beans and prints some messages containing the return values.
The entire application is packaged within a `.war` file, without any `.xml` files.
No-interface view
Each session bean exposes a no-interface Local view. This client view has the same semantics as the EJB 3.0 Local business interface, except that it does not require a separate interface. All public methods of the bean class are automatically exposed to the caller. By default, any session bean that has an empty implements clause and does not define any other Local/Remote client views exposes a no-interface client view:
`@Stateless
public class HelloBean {
public String sayHello() {
String message = propertiesBean.getProperty("hello.message");
return message;
}
}`
A client of the no-interface view always acquires an ejb reference, either through injection or JNDI lookup. The java type of the ejb reference is the bean class. The client never uses the `new()` operator to explicitly instantiate the bean class.
`@EJB
private HelloBean helloBean;
...
String msg = helloBean.sayHello();
`
Singleton bean
A Singleton bean is a new kind of session bean component that is guaranteed to be instantiated once per application in a particular JVM. A Singleton bean is defined using the @Singleton component-defining annotation.
By default, the container decides when to instantiate the Singleton bean instance. However, the developer can force the container to instantiate the Singleton bean instance during application initialization by using the `@Startup` annotation. This allows the bean to define a `@PostConstruct` method that is guaranteed to be called during startup time. In addition, regardless of eager vs. lazy instantiation, any Singleton `@PostDestroy` method is guaranteed to be called when the application is shutting down.

> `@Singleton
> @Startup
> public class PropertiesBean {
> @PostConstruct
> private void startup() { ... }
> @PreDestroy
> private void shutdown() { ... }
> ...
> }`

Deployment Descriptor
No deployment descriptors are needed, even for the Servlet. All metadata is either defaulted or specified using annotations.
Packaging
The entire application is packaged in a single `.war` file. The EJB 3.1 specification allows any enterprise bean classes to be packaged directly in `WEB-INF/classes` (or in a `.jar` in `WEB-INF/lib`) , just like other `.war` classes.
`app_dir/dist>; jar tvf ejb-ejb31-war.war`
`WEB-INF/`
`WEB-INF/classes/`
`WEB-INF/classes/ejb/`
`WEB-INF/classes/ejb/ejb31/`
`WEB-INF/classes/ejb/ejb31/war/`
`WEB-INF/classes/ejb/ejb31/war/HelloBean.class`
`WEB-INF/classes/ejb/ejb31/war/PropertiesBean.class`
`WEB-INF/classes/ejb/ejb31/war/TestServlet.class`
`WEB-INF/classes/app.properties`
Deployment
After deployment, the test servlet can be accessed at http://<host>:<http-port>/ejb-ejb31-war/

## Building, Deploying, and Running the Application

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

1. Set up 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/ejb/ejb31-war`.
3. ````Change directory to `app_dir.
   `
4. Build, deploy, and run the sample application using the `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 run` runs the test java client.

5. 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/ejb/` directory, select `ejb-war`, and click Open Project.
3. In the Projects tab, right click `ejb-war` and select Run to build, deploy, and run the project.

## Troubleshooting

If you have problems when running the application, refer the troubleshooting document.

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