Oracle9i JDeveloper - Hands on Labs (Web Services)
Modeling, Developing, Discovering, and Accessing Web Services
Introduction
This lesson demonstrates how you can visually develop J2EE web services based on Java
classes using the UML Class diagrammer. You run the web service in the embedded OC4J
application server and generate a client stub to call it, all directly from the UML Class
diagram.
The lesson then moves on to show you how you can test your web services at the network
packet level. The TCP Packet Monitor tests your web service SOAP requests and responses.
The lesson continues with the publication of a Session Bean as a web service. You then
deploy the web service and EJB to a local standalone OC4J application server instance and
test it by adding test code to a generated stub.
Web services provide an opportunity for integrating legacy business functionality
encapsulated in PL/SQL packages by publishing it as web services. This lesson shows how
simple JDeveloper makes it to wrap PL/SQL packages in Java and expose them as web services
with no knowledge of PL/SQL and no hand Java coding.
You may want to incorporate web services into a BC4J project. In this lesson you use a
web service in a BC4J project. Then you publish a BC4J custom method as a web service.
Tasks
- Develop and publish a Java class as a web service using a UML Class diagram
- Run and test the web service in the embedded OC4J
- Use the TCP Packet Monitor to test the web service at the network packet level
- Publish an EJB session bean as a web service
- Deploy the web service (and EJB) to a standalone OC4J instance
- Publish a PL/SQL package as a web service
- Use Web Services in a BC4J Application
- Develop a Web Service from a BC4J Client Method
Prerequisites
Please read the prerequisites and ensure that you have
the proper environment to complete this lab.
Develop and Publish a Java Class as a Web Service Using a UML Class Diagram
Create a New Class Diagram
- In the System Navigator, open the WebServices workspace node
- Select the JavaClassWS project
- Context menu: New UML Diagram to open the object gallery
- Choose Class Diagram
- Name the diagram JavaWS
- Accept other defaults
- Click OK to create the class diagram
The Class Diagram modeler window is open and
empty
Create a Java Class
- Select the Component Palette of the class diagram
- Select the Java palette from the drop down list
- Select Java Class from the palette
- Move to the diagram and click to create a class in the empty diagram
- The default name is highlighted. Overtype this with Person to change the name of
the new class
- In the Navigator, select the JavaClassWS project and ensure that the project is
displayed using the Categories view.
Select the icon at the top of
the navigator to toggle the view. To toggle the component palette view to icons (as shown
in the Figure 1) select Icon View from the component palette's context menu.

Figure 1: UML Class Diagram displaying Person Java class
- Open the following nodes
- Open the following nodes
- In the diagram, note the symbol in the top left corner of the Person shape. This symbol
shows that the Java code has been generated
Once generated, the modeled class and the generated Java code are synchronized and the
modeled class and Java class cannot be unsynchronized. Any change you make in the model
will be reflected in the generated code and vice-versa.You can change the generation
option for new modeled objects using the context menu of the diagram.
- Click in the first empty box in the class to add an attribute using in-place editing
- Overtype the highlighted attribute placeholder with:
+ name
- Press <enter>
Note that the type of the attribute has defaulted to String
- Click in the next box to add a method. Overtype the placeholder method signature with
the method listed below then press <enter>
+ sayHello(String p0):String
The class shape will expand and present you with a placeholder signature for the second
method
- Overtype the new method signature with the method below.
+
sayGoodbye():String
- Click the edge of the class shape to select the class. Drag it to resize as required
- Context menu: Go to source
- Add implementation code to the generated methods
public String
sayHello(String p0)
{
name = p0;
return "Hello, " + p0;
}
public String sayGoodbye()
{
return "That's all Folks, Goodbye " + name;
}
- Context menu: Rebuild Person.java
- Close the code editor to return to the class diagram.
Alternatively select the class
diagram from the Document Bar
Add a Complex Type (JavaBean) Parameter to the Class
In JDeveloper 9.0.2 you were only able to automatically consume (call and handle) web
services that returned simple types. In 9.0.3 the stub generator automatically generates
code to serialize complex types
Create the JavaBean
- In the Navigator select JavaClassWS.jpr
- Select menu option File -> New to open the Object Gallery
- Expand the Client Tier node
- Select JavaBeans
- With the Bean selected, click OK to open the JavaBean create dialog
- Give the bean the name Address
- Select java.lang.Object from the Extends list
- Click OK to create the bean
- Select Address.java in the navigator
- Context menu: Class Editor
- In the Class Editor, select the Fields tab
- Click Add and add the following fields (see Figure 2)
- Close the Class Editor
- In the Navigator select JavaClassWS.jpr
- Context menu: Rebuild JavaClassWS.jpr
Add JavaBean to the Class Diagram
- Click JavaWS in the Document Bar to make the class diagram visible in the IDE
- Drag Address.java from the Navigator onto the class diagram
A new UML shape for the
Address class is added to the class diagram
- Return to the Person class in the class diagram
- Use in-place edit to add the following public methods
+
setAddress(String town, String country):void
+ convertAddress():void
+ getAddress():Address
- Use in-place edit to add the private variable m_addr
- m_address:Address
- Select the Person class again
- Context menu: Go to source
- Amend the class to change the implementation code as follows:
private
Address m_address = new Address();
public void setAddress (String town, String country)
{
m_address.setTown(town);
m_address.setCountry(country);
}
public void convertAddress()
{
m_address.setTown("The town is " + m_address.getTown());
m_address.setCountry("The country is " + m_address.getCountry());
}
public Address getAddress()
{
return m_address;
}
- Return to the class diagram and select the Dependency arrow from the component
tool palette
- Click on Person in the diagram
- Click on Address in the diagram
This draws the dependency that the Person
class has on the Address class. No code is generated for this dependency. It is for
visualization purposes only. See Figure 3

Figure 3: UML Class diagram showing Dependency between Person and Address
classes
Publish the Person Java Class as a Web Service
- Select Person in the diagram
- Context menu: Generate -> Web Service. This launches the Web Services Publishing
Wizard
You can also invoke this wizard through the Object Gallery (File -> New)
- Step 1: Accept the default name and package
- Step 2: Click Select All to expose all the public methods in the class in the web
service
- Step 3: Accept the defaults and Finish the wizard
The web service myPersonWS
has been created and is shown in the Class diagram
- In the Navigator, select myPersonWS under the Web Services node
Note that a
WSDL document and an interface IPerson.java have been created. The interface is used by
the application server at runtime
- Select Person.wsdl
- Expand the structure window to browse the generated WSDL document
- Double-click name=sayHello to open the WSDL document at this parameter in the code
editor
- Browse the WSDL document either using the editor or the structure pane
- Close the WSDL document (do not make any changes to it!)
Run and Test the Web Service in the Embedded OC4J Server
For testing and debugging web services and other runnable applications (for instance
EJB, JSP) written in JDeveloper it is possible to 'Run' in the embedded Oracle Containers
for Java (OC4J) application server. This mimics deployng the same code to an OC4J instance
without the need for physically deploying applications or installing an OC4J instance
- Select myPersonWS in the class diagram
- Context menu: Embedded server -> Run
- This starts the embedded OC4J and 'deploys' the web service to it. When the deployment
is finished you will see the following message in the log window.
Oracle9iAS (9.0.3.0.0) Containers for J2EE initialized
Create a Sample Java Client to Test the Web Service in the Embedded OC4J
To use a web service you need to generate a client stub to call it
- Select myPersonWS in the class diagram
- Context menu: Generate -> Sample Java Client
This generates a sample Java client
called EmbeddedMyPersonWSStub to connect to the web service running in the embedded
OC4J server. It contains a main method for testing the call and response from the web
service. The other menu option under Generate generates a client stub to call the web
service from its deployed application server (taken from the WSDL file). You can also use
this stub to test the deployed web service - by adding your own main method to it or
instantiating it in your application
- In the Class diagram, select EmbeddedMyPersonWSStub
- Context menu: Go to source.
Browse down past the main method and notice that the endpoint has defaulted to the local
machine and the port that the embedded OC4J is using
- Add the following lines of code in the main method under the commented out line (see
Figure 4)
//Add your own code here
System.out.println(stub.sayHello("WS Developer"));
stub.setAddress("Reading","UK");
stub.convertAddress();
System.out.println(stub.getAddress().getTown());
System.out.println(stub.sayGoodbye());
Testing the Web Service Using the Sample Java Client
- Context menu: Run EmbeddedMyPersonStub.java
The return from the web service is displayed in the log window (see Figure 4) 
Figure 4: Log window result of running the EmbeddedPersonStub in the
embedded OC4J
Testing the Web Service at the Network Packet Level Using the TCP Packet Monitor
The TCP packet monitor allows you to monitor and test web services at the network
packet level. The HTTP requests and responses are captured and output to the monitor. Once
activated, the TCP Packet Monitor reroutes calls to a web service via an intermediate
port. You can use the TCP Packet Monitor whether you are using a proxy server or not.
Whichever is the case, the packet monitor will create appropriate 'dummy' ports.
- Menu Tools -> Preferences
- Select Web Browser/Proxy
- Note the port number of the HTTP Proxy Server
- Click Cancel to close Preferences dialog
- Menu Tools -> TCP Packet Monitor to invoke the TCP Packet Monitor
- Start the TCP Packet Monitor by clicking the green start icon (top left of monitor
window)
- Menu Tools -> Preferences
- Select Web Browser/Proxy
Note that the port number of the HTTP Proxy Server has
changed
- Cancel the Preferences dialog
- By default the monitor appears in the bottom right of the IDE. Hold down the Control Key
and drag the TCP monitor to the middle of the IDE.
- Expand it as shown in Figure 5

Figure 5: The TCP Packet Monitor
- Select the EmbeddedMyPersonStub.java code editor
- Click the Run icon in the toolbar to run the stub
- Note that as well as the expected return from the web service in the log window there
are 5 entries in the History tab of the monitor - one for each call to the web
service in the stub. The calls to the web service are:
System.out.println(stub.sayHello("WS Developer"));
stub.setAddress("Reading","UK");
stub.convertAddress();
System.out.println(stub.getAddress().getTown());
System.out.println(stub.sayGoodbye());
- Highlight the first entry in the History tab of the TCP Packet Monitor and
either double-click it or select the data tab
- Select the Switch Layout icon (5th from left) to display the request window above
the response window
- Note that the top of the request window shows the http post to the embedded
server (port 8988)
- Scroll to the bottom of the request window to show the call sayHello and the parameter
being passed (See Figure 6):
<SOAP-ENV:Body>
<ns1:sayHello xmlns:ns1="myPersonWS"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<p0 xsi:type="xsd:string">WS Developer</p0>
</ns1:sayHello>
</SOAP-ENV:Body>
- Move to the response window shows the String returned (See Figure 6)
<SOAP-ENV:Body>
<ns1:sayHelloResponse xmlns:ns1="myPersonWS"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<return xsi:type="xsd:string">Hello, WS Developer</return>
</ns1:sayHelloResponse>
</SOAP-ENV:Body>

Figure 6: TCP Packet Monitor Data tab
- In the request window change the p0 parameter value to My Name
One
powerful way to use the TCP Packet Monitor to test your web services is by changing
parameter values using the TCP Packet Monitor and resending the request. This can be
helpful when the SOAP packet is not returning the response you expected. If you have have
a complicated client that would take time to re-write this is another way of quickly
testing the expected parameters sent and received
- Click the icon Resend Request (left hand icon)
- Return to the History Tab. Highlight the new entry (the fifth entry in the window) and
return to the Data Tab
- See the new parameter that was passed in the request window
- Scroll to the bottom of the response window to see the returned greeting
- Click the Stop icon to stop the TCP Packet Monitor
- Close the TCP Packet Monitor window
Publish a Modeled EJB Session Bean as a Web Service
View EJBs in the Class Diagram
- In the Navigator, click System tab to display the System Navigator instead of the Run
Navigator
- Select the EJBWS project and ensure that the project is displayed using the Categories
view.
- Expand the following nodes
- Select EJB1
- Context menu: Class Diagram
- Note that it contains the Departments entity bean and the HRListings
session bean
Publish the Session Bean Remote Interface as a Web Service
- In the Navigator, select EJBWS.jpr
- Menu File -> New to open the object gallery
- Select Web Service in the left hand pane and Java Web Service in the right hand pane
- Step 1: Browse to the remote interface: hrpackage -> HRListings
- Step 2: Click Select All to expose all the methods in the remote interface in the web
service
- Step 3: Ensure the localoc4j Application Endpoint is selected
In this case you are going to deploy the web service and EJB to a standalone instance of
OC4J defined under the application server connections node in JDeveloper
- Accept the other defaults and Finish the wizard
Deploy the Web Service to the standalone OC4J Application Server
- In the Navigator, select WebServices.deploy
- Context menu: Settings
This is the deployment profile that holds all the information that JDeveloper needs to
deploy the web service to the application server
- Select WEB-INF Classes
Note that the WSDL document for the web service is checked for deployment
- Select Profile Dependencies
Note that the dependency of the web service on the EJB is reflected
- Click Cancel to close the deployment profile
- Context menu: Deploy to localoc4j
The Deployment log window will show the progress of the deployment with information such
as: ---- Deployment started. ---- Aug 22, 2002 11:32:07 AM
Wrote EJB JAR file to D:\903_971\jdev\mywork\WebServices\EJB\HRListingsEjbJar.jar
Wrote WAR file to D:\903_971\jdev\mywork\WebServices\EJB\WebServices.war
Wrote EAR file to D:\903_971\jdev\mywork\WebServices\EJB\test-EJB-WS.ear
Invoking OC4J admin tool...
D:\903_971\jdk\jre\bin\javaw.exe -jar D:\903_971\j2ee\home\admin.jar ormi://mymachine/
admin **** -deploy -file D:\903_971\jdev\mywork\WebServices\EJB\WebServices-EJB-WS.ear
-deploymentName WebServices-EJB-WS
Auto-unpacking D:\903_971\j2ee\home\applications\WebServices-EJB-WS.ear... done.
Auto-unpacking D:\903_971\j2ee\home\applications\WebServices-EJB-WS\WebServices.war...
done.
Copying default deployment descriptor from archive at
D:\903_971\j2ee\home\applications\WebServices-EJB-WS/META-INF/orion-application.xml to
deployment directory D:\903_971\j2ee\home\application-deployments\WebServices-EJB-WS...
Auto-deploying WebServices-EJB-WS (New server version detected)...
Copying default deployment descriptor from archive at
D:\903_971\j2ee\home\applications\WebServices-EJB-WS/HRListingsEjbJar.jar/META-INF/orion-ejb-jar.xml
to deployment directory
D:\903_971\j2ee\home\application-deployments\WebServices-EJB-WS\HRListingsEjbJar.jar...
Exit status of OC4J admin tool (-deploy): 0
D:\903_971\jdk\jre\bin\javaw.exe -jar D:\903_971\j2ee\home\admin.jar ormi://mymachine/
admin **** -bindWebApp WebServices-EJB-WS WebServices http-web-site
/WebServices-EJB-context-root
Exit status of OC4J admin tool (-bindWebApp): 0
Use the following context root(s) to test your web application(s):
http://mymachine:8888/test-EJB-context-root
Elapsed time for deployment: 1 minute, 42 seconds
---- Deployment finished. ---- Aug 22, 2002 11:33:49 AM
Create a Client-side Stub for the Web Service
- In the Navigator, select EJBWS.jpr
- Menu File -> New to open the new object gallery
- Select Web Services in the left hand pane and Web Service Stub/Skeleton in
the right hand pane
- Complete the Web Service Stub/Skeleton wizard
- Step 1: Select HRListings from the drop down list
- Under Generate Options, check Generate Main Method into Stub
- Accept all other defaults and click Finish to create the stub
The stub HrpackageHRListingsStub.java is created and opened in a code editor
- Scroll down past the main method to the endpoint
The endpoint is taken from the WSDL document and shows the location of the standalone
local OC4J server that you specified
- Add the following code to the main method to instantiate the stub for testing
// Add your own code here.
System.out.println(stub.listDepartments());
- Context menu: Rebuild HrpackageHRListingsStub.java
Call the deployed web service using the generated stub
- Context menu: Run HrpackageHRListingsStub.java
- Note the return from the web service in the log window, similar to:
Department
Listing ....
10......Administration...1700....
20......Marketing...1800....
30......Purchasing...1700....
40......Human Resources...2400....
Process exited with exit code 0.
Publish a PL/SQL Package as a Web Service
Using a database connection defined in JDeveloper you can browse your database packages
and automatically create a Java wrapper and a web service to expose existing database
functionality as a web service
- In the Navigator, select PLSQL.jpr
- Go to the Connections node
- Expand the following nodes:
- Database
- hrConnection (database node)
- hr (schema node)
- packages
- GETDATETIME
- Double-click the package body and use the code editor to see the function return (see
Figure 7)
return 'Hello ' || name || ', current date and time is ' ||
bind.value;
This is a very trivial example, designed to show the principle not a great business
logic example

Figure 7: Database Connections node in the Navigator
- In the Navigator, select the GETDATETIME package
- Context menu: Publish as Web Service
The PL/SQL Web Service Publishing Wizard is
opened
- Step 1: Select the PLSQL project from the list of projects, accept other defaults
- Step 2: Select All to select the program units to be exposed as web services
- Step 3: Accept all the defaults and Finish the wizard
The PL/SQL Web Service Publishing Wizard uses JPublisher to create a Java wrapper for
the package.
- In the Navigator, Browse to the PLSQL project and review the generated WSDL document
(Getdatetime.wsdl) and the Getdatetime.java class
The WSDL document gives no indication that the underlying implementation of the web
service is a PL/SQL package
The Java wrapper class handles all the complexity of communicating with the package in
the database
Run the web service using the embedded OC4J server
- If the embedded OC4J is running, terminate it: Run -> Terminate ->...
- Ensure that the PLSQL project is displayed using the Category view
- Under the Web Services node, select the hrconnection.GetdatetimeWebService web
service container
- Context menu: Run
Create a sample java client to test the PL/SQL web service using the embedded OC4J
- Select the container hrconnection.GetdatetimeWebService web service container
- Context menu: Generate Sample Java Client
- In the code editor, scroll down the EmbeddedHrconnectionGetdatetimeWebServiceStub.
java to the main method and add the following line under the commented out line
// Add your own code here.
System.out.println(stub.gettime("User"));
- Run the stub and see the output similar to the following in the log window
Hello User, current time is 28-AUGUST-2002 17:31:19
Process exited with exit code 0.
Use Web Services in a BC4J Application
In this task you use the CreditServices web service to return the credit history
of every customer in a BC4J application module and test it in the BC4J tester. You then go
on to create a JSP front end for the BC4J application and add the DateTimeServices
web service to the JSP
Incorporate the CreditServices Web Service in a BC4J Application
Review the CreditServices Session Bean
- In the Navigator, ensure that the BC4J.jpr project is displayed using the Categories view.
expand the following nodes
- BC4J.jpr
- Enterprise JavaBeans
- ejb-jar.xml
- CreditServices
- Open CreditServicesBean.java in the code editor
Note that the bean contains
the method CreditHistoryCheck that returns the credit history of a customer
Review the CreditServices Web Service
- In the Navigator, expand the following nodes
- BC4J.jpr
- Web Services
- orderentrypackage.CreditServices
- Double-click orderentrypackage.CreditServices to open the re-entrant wizard
Note that
this web service is a publication of the Session Bean CreditServices
- Select the Exposed Methods tab
Note that the method CreditHistoryCheck has
been published
- Click Cancel to close the wizard and make no changes
- Terminate any embedded OC4J session running (Run -> Terminate -> ...)
- In the Navigator, select orderentrypackage.CreditServices
- Context menu: Run orderentrypackage.CreditServices
This runs the session bean and the
web service in the embedded OC4J application server.
Review the EmbeddedCreditServicesStub
- In the Navigator, expand the following nodes
- BC4J.jpr
- Sources
- orderentrypackage
- Open EmbeddedCreditServicesStub.java in the code editor
Note that it is a stub
to run against the embedded OC4J application server to call the CreditServices web service
Add a Transient Attribute to the BC4J View Object to Call the Web service

Figure 8: Create a new view object attribute
- In the Navigator, expand the following nodes
- BC4J.jpr
- Business Components
- orderentrypackage
- Select CustomersView
- Context menu: Edit Customers View
- Select Attributes in the left hand panel
- Select New in the right hand panel to add a new View Object Attribute (see figure 8
above)
- Name: CreditHistory
- Updateable: Always
- Accept other defaults
- Select OK
- Select Java in the left hand panel
- Select Generate Java File for View Row Class:CustomersViewRowImpl
- Select OK to close wizard
CustomersViewRowImpl.java is generated
- Save All
Add Code to Call The Web Service from the Customers View Object
- Select CustomersViewRowImpl.java in the code editor
- Edit method getCreditHistory as follows
public String
getCreditHistory()
{
String ws = null;
try
{
EmbeddedCreditServicesStub stub = new EmbeddedCreditServicesStub();
ws = stub.CreditHistoryCheck(getCustomerId().toString());
} catch (Exception ex)
{
ex.printStackTrace();
ws = "cannot access credit history";
} finally
{
return ws;
}
// return (String)getAttributeInternal(CREDITHISTORY);
}
- Rebuild code
Test the Web Service Using the BC4J Tester
- In the Navigator, under Business Components, select OrderEntryAppModule
- Context menu: Test
- Accept defaults and select Connect
- Select Customers View
- Context menu: Show
The web service returns a poor CreditHistory for customer 101
- Select Specify View Criteria icon
- Enter 4% in CustomerId
- Select Find
The web service returns records for customers with a customer_id that begins with 4. Their
CreditHistory should be good
- Close the tester
Create a Web Service from a BC4J Client Method
In this task you expose a BC4J client method as a web service.
Add a Client Method to the Application Module
- In the Navigator, open the following nodes
- BC4J.jpr
- Business Components
- orderentrypackage
- OrderEntryAppModule
- Select OrderEntryAppModuleImpl.java
- Context menu: Code Editor
- Add the following import statement to the list of import statements at the top of the
class
import oracle.jbo.*;
- Add the following method to the class
public String getOrdDetails(String
s)
{
ViewObject ordvo = findViewObject("OrdersView");
String whereclause = "ORDER_ID = " + s;
ordvo.setWhereClause(whereclause);
Row r = ordvo.first();
if (r == null) return "<Not Found>";
String result = (r.getAttribute("OrderId")).toString() + " "
+ (r.getAttribute("CustomerId")).toString() + " "
+ (r.getAttribute("OrderTotal")).toString();
return result;
}
- Build the class
- Select OrderEntryAppModule in the Navigator
- Context menu: Edit OrderEntryAppModule
- Select Client Methods tab
- select getOrdDetails from the Available list
- Move to the Selected list (See Figure 9)
- Click OK to close dialog
- Select OrderEntryAppModule in the Navigator
- Context menu: Generate Web Service Class
Wrapper class OrderEntryAppModuleWS.java is generated under the Sources node. Note:
this menu option is not available if steps 8-10 have not been done
- Select BC4J.jpr
- Context Menu: New to go to the new object gallery
- Select Web Services from the Categories panel and Java Web Service from the Items panel.
- This opens a Wizard
- Step 1: Browse and select OrderEntryAppModuleWS from the orderentrypackage.
Accept the other defaults
- Step 2: Click Select All to include all the exposed methods in the web service
- Step 3: Ensure that the Application Server Endpoint is WSConnection. Accept the
other defaults finish the wizard.
The web service should have been generated and the OrderEntryAppModuleWS.wsdl file
should be open in the code editor.
To run this web service it would be necessary to deploy the BC4J application to the
OC4J instance. This is outside the scope of this lesson.

Figure 9: Move the client method to the Selected list
|