This tutorial covers creating an EJB 3.1 singleton
session bean and exposing the singleton session bean as a
JAX-WS web service and consuming the web service using
different types of web service clients.
Time to Complete
Approximately 45 minutes.
This example demonstrates the use of the EJB 3.1 singleton
session bean and exposing the singleton session bean to create a
JAX-WS web service and also illustrates creating web service
clients to consume the web service. EJB 3.1 specification allows
EJB's to be developed in a web application.
Java EE 6 web services technologies include Java API for XML Web Services (JAX-WS) and Java API for RESTful Web Services (JAX-RS). JAX-WS simplifies the task of developing web services using Java technology.
Singleton session bean:
Singleton session beans were introduced as part of the EJB
3.1 specification. They are instantiated only once per
application and exist for the life cycle of the application.
They support concurrent access. Singleton session beans provide
shared data access to clients and components within an
application. It maintains its state between client invocations
but that state is not required to survive container shutdown or
crash. Singleton session beans are defined using the
annotation. In cases where the container is distributed over
many virtual machines, each application will have one bean
instance of the singleton for each JVM.
Web Service Clients:
Stateless session beans and Singleton session beans may have web service clients. A web service client accesses a session bean through the web service client view. The web service client view is described by the WSDL document for the web service that the bean implements. WSDL is an XML format for describing a web service as a set of endpoints operating on messages. The abstract description of the service is bound to an XML based protocol (SOAP) and underlying transport (HTTP or HTTPS) by means of which the messages are conveyed between client and server.
The web service client view of an enterprise bean is location
independent and remotable. Web service clients may be Java
clients or clients not written in the Java programming language.
A web service client that is a Java client accesses the web
service by means of the JAX-WS or JAX-RPC client APIs.
In this tutorial, you will create a Java EE 6 Web Application
and add the following components to it - a singleton
CounterBean and implement a
getHits() method in it. The singleton session bean
provides a central counter service. Next you will expose
the singleton session bean as a JAX-WS web service called
CounterService. You will deploy the web service to
GlassFish server and test the web service You will develop
two clients to consume this web service - a Java class in
a Java SE application and a jsp page in a web
Hardware and Software Requirements
The following is a list of hardware and software requirements:
- Download and install Java JDK 7 from this link.
- Download and install NetBeans 7.1.2 with Java EE which includes GlassFish 3.1.2 (Java EE download bundle) from this link. During installation, be sure to check the box to install GlassFish. JUnit is an optional installation and not required for this tutorial.
Before starting this tutorial, you should:
- Have the software installed as listed under Hardware and Software Requirements section.
- Ensure NetBeans is running.
Creating a Web Application
To create a Java EE Web Application, perform the
following steps in the NetBeans IDE.
Creating a Singleton Session Bean
This section demonstrates the creation of an EJB 3.1 singleton session bean.
Creating a Web Service
To create a JAX-WS web service perform the below steps in NetBeans IDE.
Deploying and Testing the Web Service
To deploy and test the web service, perform the following steps
in NetBeans IDE. You can follow the progress of these operations
EJBWebServicesDemo and the
server tabs in the Output window.
Consuming the Web Service
Now that you have deployed the web service, you need to create
a client to make use of the web service's
method. In this section you create two clients— a Java
class in a Java SE application, and a JSP page in a web
Client 1: Java Class in Java SE Application
In this section you create a stand-alone
application client that accesses the getHits
method of CounterService web service.
Client 2: JSP page in a Web Application
In this section, you create web application and
accesses the getHits method of CounterService
web service in the default JSP page,
In this tutorial, you have learned how to:
- Create JAX-WS web service
- Create a singleton session bean
- Expose the singleton session bean as a JAX-WS web
- Deploy and test web service
- Create web service clients - Java SE client and web
- JSR 318: Enterprise JavaBeans 3.1
EE 6 Tutorial
- Enterprise JavaBeans Technology
- Creating an Enterprise Application with EJB 3.1
EE 6 Web Service Tutorial
- To learn more about Java Enterprise Edition refer to additional OBEs in the Oracle Learning Library.
- Lead Curriculum Developer: Anjana Shenoy
To help navigate this Oracle by Example, note the following:
- Hiding Header Buttons:
- Click the Title to hide the buttons in the header. To show the buttons again, simply click the Title again.
- Topic List Button:
- A list of all the topics. Click one of the topics to navigate to that section.
- Expand/Collapse All Topics:
- To show/hide all the detail for all the sections. By default, all topics are collapsed
- Show/Hide All Images:
- To show/hide all the screenshots. By default, all images are displayed.
- To print the content. The content currently displayed or hidden will be printed.
To navigate to a particular section in this tutorial, select the topic from the list.