This sample demonstrates injection of an EJB into a servlet. The application consists of a servlet and stateless session bean. The servlet uses annotations to inject the stateless session bean, and then performs a call on the hello method of the bean.
This sample can be installed onto runtime versions 8.5.5.0 and later.
The Liberty Maven plug-in and WebSphere Developer Tools support creating loose applications. This example creates a loose EAR application when building using Maven. After running the full build, you will see the application installed as sample.ejb/ejb-ear/target/liberty/wlp/usr/servers/ejbServer/apps/ejb-ear.ear.xml
.
- Clone this project and import into Eclipse as an 'Existing Maven Project'.
- Right-click the project and select Run As > Maven Clean.
- Right-click the project and select Run As > Maven Install.
- Right-click the project and select Run As -> Run on Server.
- You should see the following in the console:
Application EJBSample started in XX.XX seconds.
This project can be built with Apache Maven. The project uses the Liberty Maven Plug-in to automatically download and install the Liberty Java EE 7 Full Platform 7 runtime from Maven Central. The Liberty Maven Plug-in is also used to create, configure, and run the application on the Liberty server.
Use the following steps to run the application with Maven:
-
Execute the full Maven build. The Liberty Maven Plug-in will download and install the Liberty server in the
ejb-ear
project. It will also run all tests.$ mvn clean install
-
To run the server in the
ejb-ear
subproject:$ mvn --projects ejb-ear liberty:run-server
The
--projects
and-pl
are equivalent options.
In your browser, enter the URL for the application: http://localhost:9080/ejb-war/ejbservlet/ In your browser, you should see the message "Hello EJB World".
This project can also be built with Gradle. The project uses the Liberty Gradle Plug-in to automatically download and install the Liberty Java EE 7 Full Platform runtime from Maven Central. The Liberty Gradle Plug-in is also used to create, configure, and run the application on the Liberty server.
The Liberty Gradle Plug-in supports creating loose application configuration for war tasks, but it does not yet support loose enterprise applications. This example installs the EAR file when building using Gradle. After running the full build, you will see the application installed as sample.ejb/ejb-ear/build/wlp/usr/servers/ejbServer/apps/ejb-ear.ear
.
- Go to Help > Eclipse Marketplace > Install Buildship Gradle Integration 2.0
- Clone this project and import into Eclipse as an 'Existing Gradle Project'.
- Go to Window > Show View > Other > Gradle Executions & Gradle Tasks
- Go to Gradle Tasks view and run
clean
in build folder, thenbuild
in build folder, thenlibertyStart
in liberty folder. - You should see the following in the console:
Application EJBSample started in XX.XX seconds.
Use the following steps to build and run the application with Gradle from the root project:
-
Execute the full Gradle build. The Liberty Gradle Plug-in will download and install the Liberty server in the
ejb-ear
project. It will also run all tests.$ ./gradlew clean build
-
To start the server with the EJB sample application run:
$ ./gradlew libertyStart
Alternatively, execute the run command:
$ ./gradlew libertyRun --no-daemon
-
To stop the server, run:
$ ./gradlew libertyStop
In your browser, enter the URL for the application: http://localhost:9080/ejb-war/ejbservlet/ You should see the message "Hello EJB World".
© Copyright IBM Corporation 2017.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.