Sample Application using JAX-WS, JSF, EJB 3.0, and Java
Sample Application using JAX-WS, JSF, EJB 3.0,
and Java Persistence APIs on Glassfish
eBay and Amazon provide Web Services APIs for developers who want to
provide access to these services in their web site. This Sample Store
Catalog application shows how to expose a
Service as a Web Service for remote client applications (this example
was not modeled after the eBay or Amazon APIs).
This example demonstrates a Catalog Stateless Session
Bean, and the Java Persistence APIs to implement
Catalog
Service which provides pagination of store items, and JAX-WS to
expose this Catalog
Service as a Web Service. A
separate example JSF JAX-WS client shows how this Catalog Web Service
can then
be used remotely in a sample Store web site. I took this
example
href="http://weblogs.java.net/blog/caroljmcdonald/archive/2007/05/pagination_of_d.html">Pagination
of Data Sets in a Sample Application using JSF, Catalog Facade
Stateless Session, and Java Persistence APIs and modified the
Catalog Session Bean to expose its public methods as Web Services, then
I put the JSF Store UI in a separate Web Application and modified it to
use
JAX-WS to call the Catalog Web Services.
href="https://techdayscode.dev.java.net/servlets/ProjectDocumentList?folderID=8050&expandFolder=8050&folderID=8050">Download
the Sample Application Code
Explanation of the usage of JAX-WS, JSF, EJB 3.0, and
Java Persistence
APIs in a sample Store Catalog Application
The image below shows the Catalog Listing page, which allows a user to
page through a list of items
in a store.
Explanation of the Catalog Web Service which uses JAX-WS, JPA,
and EJB 3.0
The Catalog
Session EJB uses a JPA EntityManager
Query
style="color: rgb(204, 0, 0); font-weight: bold;">object to return a list of
style="font-weight: bold; color: rgb(0, 102, 0);">items. style="color: rgb(204, 0, 0);"> style="font-weight: bold; color: rgb(204, 0, 0);"> style="color: rgb(204, 0, 0); font-weight: bold;">Withthe
style="color: rgb(204, 0, 0);">@PersistenceContext annotation,the CatalogBean uses dependency injection to lookup and obtain a
Container Managed
style="font-weight: bold;">EntityManager
style="color: rgb(204, 0, 0);">
style="font-weight: bold; color: rgb(204, 0, 0);">
style="color: rgb(204, 0, 0);">
style="font-weight: bold;">
style="color: rgb(204, 0, 0);"> style="font-weight: bold; color: rgb(204, 0, 0);">. The
@Stateless
annotation marks this class as a Stateless Session EJB.The
@WebServiceannotation marks this class as a web service, and causes any public
methods to be exposed as Web Services. The example JSF Web
Service client uses the
style="color: rgb(0, 102, 0); font-weight: bold;">CatalogWeb Service
style="font-weight: bold; color: rgb(0, 102, 0);">getItemsmethod to get the
style="font-weight: bold; color: rgb(0, 102, 0);">Itemsfor displaying on the Web Store UI . (Note: style="color: rgb(204, 0, 0); font-weight: bold;">Red colors
are for Java EE
tags, annotations code, style="color: rgb(0, 0, 153); font-weight: bold;">
and Green
for my code
or variables)
Code Sample from: .java |
|
The code below shows the
style="color: rgb(0, 102, 0); font-weight: bold;">Itementity class which maps to the ITEM table that stores the
item instances. This is a
typical Java Persistence entity object. For more information on
this code see href="http://weblogs.java.net/blog/caroljmcdonald/archive/2007/05/pagination_of_d.html">this
previous blog.
Code Sample from: .java |
@Id
|
When this application is deployed, the JAXB, JAX-WS, and
WSDL files needed for the Service will be generated on the
server. You can access the
style="color: rgb(0, 102, 0); font-weight: bold;">Catalog WSDLcontract at this URL: http://host:8080/CatalogService/Catalog?wsdl
. Below is part of the WSDL, you can see that the
style="color: rgb(0, 102, 0); font-weight: bold;">Catalog classname defaults to the
style="font-weight: bold; color: rgb(0, 0, 153);">portTypename and the
style="color: rgb(0, 102, 0); font-weight: bold;"> style="font-weight: bold; color: rgb(0, 102, 0);">getItems methodname defaults to the
style="color: rgb(0, 0, 153); font-weight: bold;">operationname (defaults can be changed using annotations, see href="http://java.sun.com/javaee/5/docs/tutorial/doc/bnayk.html">the
Java EE tutorial for more information).
style="color: rgb(0, 102, 0); font-weight: bold;">| Code Sample from: CatalogService.wsdl |
|
style="font-weight: bold; color: rgb(0, 102, 0);">Hereis part of the generated xml Schema for the WSDL getItems response
message:
| Code Sample from: CatalogService.xsd |
|
and the corresponding generated JAXB class:
Code Sample from: .java |
|
JAX-WS delegates all data binding functionality to JAXB 2.0:
style="width: 622px; height: 414px;"/>
After deployment on Glassfish you can access a web client tester
application provided by the
Glassfish Application Server at the URL for the Web Service
followed by "?Tester" for example :
http://host:8080/CatalogService/Catalog?Tester. Below is the Web
Service Tester interface for the CatalogService. It provides an easy
way to call the Web Service operations from a browser.

Here is an example soap request and response for the getItems
WebService operation:
SOAP Request
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Header/>
<S:Body>
<ns2:getItems xmlns:ns2="http://model.sessionpagination/">
<arg0>0</arg0>
<arg1>2</arg1>
</ns2:getItems>
</S:Body>
</S:Envelope>
SOAP Response
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:getItemsResponse xmlns:ns2="http://model.sessionpagination/">
<return>
<address>
<addressid>1</addressid>
<city>Mountain View</city>
<latitude>37.3857400000</latitude>
<longitude>-122.0839730000</longitude>
<state>CA</state>
<street1>Castro St</street1>
<street2/>
<zip>94040</zip>
</address>
<contactinfo>
<contactinfoid>1</contactinfoid>
<email>abc@abc.xyz</email>
<firstname>Sean</firstname>
<lastname>Brydon</lastname>
</contactinfo>
<description>super friendly</description>
<itemid>1</itemid>
<name>Friendly Cat</name>
<numberofvotes>3</numberofvotes>
<price>307.10</price>
<product>
<category>
<categoryid>CATS</categoryid>
<description>Loving and finicky friends</description>
<name>Cats</name>
</category>
<description>Great for reducing mouse populations</description>
<name>Hairy Cat</name>
<productid>feline01</productid>
</product>
<totalscore>15</totalscore>
</return>
<return>
<address>
...
</address>
<contactinfo>
...
</contactinfo>
<itemid>2</itemid>
<name>Fluffy Cat</name>
<product>
<category>
<categoryid>CATS</categoryid>
...
</category>
<name>Hairy Cat</name>
<productid>feline01</productid>
</product>
</return>
</ns2:getItemsResponse>
</S:Body>
</S:Envelope>
Explanation of the JSF Store UI which uses JAX-WS to call the
Catalog Web Service.
The JSF Store UI is a separate web application which is a JAX-WS
client.

The List.jsp page uses a JSF dataTable
component to display a list of
catalog items. The dataTable component is useful when you want to show
a set of
results in a table.
In the List.jsp
web page the dataTable is defined as shown below:
List style="color: rgb(0, 0, 0);">.jsp
<h:dataTable style="color: rgb(204, 0, 0);"> style="font-weight: bold; color: rgb(204, 0, 0);">value=' style="font-weight: bold;">#{ style="color: rgb(0, 102, 0); font-weight: bold;">item.items style="font-weight: bold;">}' style="font-weight: bold; color: rgb(204, 0, 0);">var=' style="color: rgb(0, 102, 0); font-weight: bold;">dataTableItem'
border="1"
cellpadding="2" cellspacing="0">
The value attribute of a dataTable
tag references the data to be included
in the table. The var
attribute points
to a single item in that list. As the
dataTablestyle="font-weight: bold;"> points to the current item in theJSF component iterates through the
list, each reference tostyle="color: rgb(0, 102, 0); font-weight: bold;" class="cCode">dataTableItem
list.
The
dataTable's valueis bound to the style="font-weight: bold;">
style="color: rgb(0, 102, 0); font-weight: bold;">itemsproperty
of the
ItemControllermanaged-bean
style="font-weight: bold;"> class,
style="font-weight: bold;">which is defined in thefaces-config.xml
| Code Sample from: faces-context.xml |
|
This ItemController ManagedBean
style="color: rgb(0, 102, 0);">itemsproperty is defined as shown below (the style="color: rgb(0, 0, 153); font-weight: bold;">blue color
highlights the JAX-WS
dynamic proxy classes and methods):
public class style="color: rgb(0, 102, 0); font-weight: bold;">ItemController
{
@WebServiceRef(wsdlLocation
= "http://localhost:8080/CatalogService/Catalog?wsdl")
private CatalogService
service;
public style="color: rgb(204, 0, 0); font-weight: bold;">DataModel style="color: rgb(0, 102, 0); font-weight: bold;">getItems() {
if (model==null || index !=
firstItem){
model=getNextItems();
}
return this.model;
}
public DataModel
getNextItems() {
style="color: rgb(0, 0, 153); font-weight: bold;">Catalog style="font-weight: bold;">port = style="color: rgb(0, 0, 153); font-weight: bold;">service.getCatalogPort();
model = new style="color: rgb(204, 0, 0); font-weight: bold;">ListDataModel( style="font-weight: bold;">port. style="color: rgb(0, 0, 153); font-weight: bold;">getItems(
firstItem,batchSize));
return model;
}
The
style="color: rgb(0, 102, 0); font-weight: bold;">ItemController
ManagedBean uses dependency injection to obtain a reference to the
style="font-family: monospace;">
style="font-weight: bold; color: rgb(0, 0, 153);">CatalogService
JAX-WS proxy factory class, which is generated from the Catalog WSDL
file using the wsimport utility. (To see how to do this easily with
Netbeans see the
href="http://www.netbeans.org/kb/55/websvc-jax-ws.html">Netbeans Web
Services (JAX-WS) in Java EE 5 tutorial). The
style="font-family: monospace;">
style="color: rgb(0, 102, 0); font-weight: bold;">ItemController
retrieves a proxy to the service by calling
style="color: rgb(0, 0, 153); font-weight: bold;">getCatalogPort()on the style="font-weight: bold; color: rgb(0, 0, 153);">CatalogService,
which returns the
style="color: rgb(0, 0, 153); font-weight: bold;">Catalog ServiceEndpoint Interface. The proxy implements the
style="color: rgb(0, 0, 153); font-weight: bold;">Catalog ServiceEndpoint Interface defined by the
style="color: rgb(0, 0, 153); font-weight: bold;">Catalog service. The style="color: rgb(0, 102, 0); font-weight: bold;">ItemController can
then invoke the port’s
style="color: rgb(0, 0, 153); font-weight: bold;">getItemsmethod. The dynamic proxy and jaxb classes convert the WS method
into a SOAP request and send it to the Web service's endpoint,
receive the SOAP response, and transform the SOAP response into the
java method's return object which in this case is a
style="color: rgb(0, 102, 0);">List< style="color: rgb(0, 102, 0); font-weight: bold;">Item style="color: rgb(0, 102, 0);">> style="color: rgb(0, 102, 0); font-weight: bold;">. The
getItems()method wraps a List of items, returned from the
style="color: rgb(0, 0, 153); font-weight: bold;">Catalog Service, in a
DataModel. The
dataTable JSF
component supports data binding to a
collection of data objects represented by astyle="font-weight: bold; color: rgb(204, 0, 0);">DataModel
instance. The data
collection underlying a DataModel instance is modeled as a collection
of row objects that can be accessed by a row index. The APIs
provide mechanisms to position to a specified row index, and to
retrieve an object that represents the data that corresponds to the
current row index.
The Name, Photo, and Price
itemproperties are displayed with the
JSF
columncomponent:
| Code Sample from: List.jsp |
|
The column tags represent columns of data in a dataTable.
The dataTable JSF component iterates
through the list of items
(item.
style="color: rgb(0, 102, 0);">items) each time renderingone cell in
each column and displaying the
style="color: rgb(0, 102, 0);">item name, photo, andprice.
For more information on
the JSF part of this code see href="http://weblogs.java.net/blog/caroljmcdonald/archive/2007/05/pagination_of_d.html">this
previous blog.
Conclusion
The sample Store Catalog application demonstrates how to
expose
EJB 3.0 style="color: rgb(204, 0, 0); font-weight: bold;">Stateless Session EJB
methods
which use the
Java
Persistence APIs as a Web Service operations using style="color: rgb(204, 0, 0); font-weight: bold;">JAX-WS.
The sample JSF Store UI application demonstrates how to use
the JSF style="font-weight: bold;"> style="color: rgb(204, 0, 0); font-weight: bold;">dataTable and
DataModel
to page through a
list
of Items
which are retrieved from the
Catalog Serviceusing JAX-WS.
Running the Sample Application on
Glassfish:
-
href="https://glassfish.dev.java.net/public/downloadsindex.html">Download
and install GlassFish V2, following the instructions on the download
page. Alternatively you can use Sun Java System Application Server PE
9, Sun's GlassFish distribution. - Download
and install NetBeans 5.5.1 -
href="https://techdayscode.dev.java.net/servlets/ProjectDocumentList?folderID=8050&expandFolder=8050&folderID=8050">Download
the Sample Application Code - install Glassfish and Netbeans 5.5.1. Then add the
glassfish application server to Netbeans.
To Open and Test Run the sessionpagination Project:
- Open the Netbeans sessionpagination project: In Netbeans under
File Open Project... go to the directory where you unzipped the sample
and select the sessionpagination project. - If you get a message that says unresolved references, right click
on the project and select Resolve Reference Problems. Use the Resolve
Reference Problems dialog to map the ejb and web
modules to their
project, which are subdirectories beneath the sessionpagination
directory. - After the references are resolved, right-click the
sessionpagination
project and select Open Required Projects. - If the web module says unresolved references, right-click the
sessionpagination-Web module and select Resolve
Reference
Problems: - Browse to the sessionpagination-ejb directory which is a
sub-directory below the sessionpagination directory and select Open
Project
Folder. - If you don't have any resolve reference problems errors then
ignore those steps.
- Starts the application server, or at least connect to the
database, because the run script for this application will also create
the database tables, and this will fail if the database is not started.
- Right-click the project node and choose Run Project.
The Netbeans IDE starts the application server, builds the application,
and opens the web context page in your browser. This application also
has a local JSF client in the war of the application which will be
displayed.
- To go to the web client Tester
application provided by the
Glassfish Application Server use the url :
http://host:8080/CatalogService/Catalog?Tester. You should see the
tester page. For the getItems operation type in integer the integers 0,
5 as input and click on the getItems button. This will return a list
of items 0 through 5.
To Open and Test Run the sessionpagination-wsclient Project:
- Open the Netbeans sessionpagination-wsclient project: In Netbeans
under File
Open Project... go to the directory where you unzipped the sample and
select the sessionpagination-wsclient project. - Right-click the project node and choose Run Project.
The Netbeans IDE builds the application,
and deploys it.
- When you run the project, your browser should display the opening
page
of the application at http://localhost:8080/sessionpagination-wsclient/

References:
-
href="https://blueprints.dev.java.net/bpcatalog/ee5/persistence/index.html">Java
BluePrints Solutions Catalog for the Java Persistence APIs
contains a collection of topics and example applications. -
target="bpcatalog">Java
Persistence reference page on GlassFish Project - Java
EE tutorial, good tutorial on JSF , JPA, EJB, and JAX-WS - Pro
EJB 3: Java Persistence API book - Netbeans
Web Services (JAX-WS) in Java EE 5 tutorial
- Login or register to post comments
- Printer-friendly version
- caroljmcdonald's blog
- 15163 reads






Comments
hi carol i have
by ahsansadeque - 2011-03-03 21:14
hi carol
i have been trying to open the sample download link but it does not seem to open , either the adress of the link has been permenantly changed if so please send me the new address.
Since i am new with web services. Can u tell me please that the above tutorial explains both server and client app of the catalog example or there are different tutorials
thanking in advance
How to use the same JPA entities classes in a client app.?
by xcallejas - 2010-02-09 11:44
Hi, I must consume a JAX-WS web service, in my stand-alone client application I have created a client for the web service with the Netbeans Web Service client wizard, this client application project have imported the library witch contains my JPA entites (the same that use the web service in the EJB module), I need that the web service client use these jpa entites and not the ones it generates (jaxb ones). Is this possible???running the client (JSF) and server (Spring and JAX-WS services)
by srini_chennamaraja - 2009-11-04 09:36
Hi, I was wondering how would you approach if I had to run the client (JSF) and the server (Spring and JAX-WS service implementations) on two different jvms. Would you recommend using the same domain model on the client side?. Would the sample work if I split up the client and service on two different servers (physical). I was not able to run the sample that way... thanks SriniIt should run fine on 2
by caroljmcdonald - 2009-11-04 12:45
It should run fine on 2 different servers, that's the point. The objects that get passed are in the WSDL and JAXB objects are generated in the client stubs from that . Just make sure you set the URL for the web service in the wsdl correctly , in the sample it is set for localhost
(No subject)
by caroljmcdonald - 2009-11-04 12:46