
Exposing and Consuming Web Services with NetBeans IDE 4.0
Web services are distributed application
components that conform to standards that make them externally available.
The proliferation
of distributed environments has created a need for an enterprise to be able to expose all
or part of the functionality of an application to other applications over an open network. Web services solve
the problem of integrating applications that have been developed
independently and run on a variety of software and hardware platforms.
Even though web service support is not bundled with NetBeans IDE 4.0, this tutorial shows
you how you can manually integrate web service functionality provided by JAX-RPC, which is a subset of the
Java Web Services Developer Pack 1.5 (JWSDP).
The next NetBeans IDE version (NetBeans IDE 4.1) comes bundled with web service support based on JSR-109,
which is a development paradigm that is suited for J2EE, based on JAX-RPC.
Web service functionality in NetBeans IDE 4.1 will be one part of an
end-to-end set of J2EE features. Working with web services will also be a lot easier than in NetBeans IDE 4.0.
For example, NetBeans IDE 4.1 will provide wizards to create web services
and web service clients, just as there are wizards to create servlets and JSP files, and you will not
have to do most of the manual steps described below.
In this tutorial, you create a Java application that consumes a JAX-RPC web service deployed on the IDE's bundled
Tomcat web server. You develop the
web service and its client from scratch. During the course of the tutorial, you can familiarize yourself with the JWSDP's web service
functionality upon which NetBeans IDE 4.1 will be based.
This tutorial covers the following topics:
This tutorial can be completed in 60 minutes.
Set Up Your Resources
Set Up NetBeans IDE 4.0
Go to http://www.netbeans.org.
Download, install, and start NetBeans IDE 4.0.
Set Up JWSDP
Go to http://java.sun.com/webservices/downloads/webservicespack.html.
Download and unzip the Java Web Services Developer Pack (JWSDP) 1.5 to any folder on your computer.
From now on, we will refer to this folder as $JWSDPDIR.
NetBeans IDE 4.1 will not require you to download the JWSDP. All necessary JAR files will be bundled with the IDE.
Developing the Web Service
Createthe Web Application Project
Choose File > New Project (Ctrl-Shift-N).
Under Categories, select Web. Under Projects, select Web Application.
Under Project Name, type JaxrpcWS.
- Change the Project Location to any folder on your computer. Click Finish.

NetBeans IDE 4.1 will let you implement the web service either within an EJB module or, as in this example,
within a web application.
The project opens in the IDE. You can view its logical structure in the Projects window
and its file structure in the Files window. For example, when the web service is finished, at the end of this section,
the Projects window should look as follows:
Setthe Classpath
Right-click the JaxrpcWS project node in the Projects
window and choose Properties. In the Project Properties dialog box,
select Compiling Sources, click Add Library, and then Manage Libraries.
In the Library Manager, click New Library and type JAX-RPC in the Library Name
text field. Click OK. Select JAX-RPC and click Add JAR/Folder. Browse to $JWSDPDIR\jaxrpc\lib
and to $JWSDPDIR\saaj\lib, select all the JAR files, then select activation.jar and mail.jar from
$JWSDPDIR\jwsdp-shared\lib. Click Add JAR/Folder for each. The Classpath pane shows the JAR files that
can be added to the project. You should have the following selected:

NetBeans IDE 4.1 will bundle these JAR files with the IDE.
Click OK to add the JAR files to the library.
Make sure the JAX-RPC library is selected and click Add Library.
Click OK to add the library to the project.
Create the Web Service
Right-click the Source Packages node and choose New > Java Class.
Name the class HiSEI, type org.me.hi in the
Package field, and click Finish. HiSEI.java opens in the
Source Editor. Replace the content of HiSEI.java with the following code:
package org.me.hi;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface HiSEI extends Remote {
public String sayHi(String s) throws RemoteException;
}
Implement the interface by creating the HiImpl.java file
with the following code:
package org.me.hi;
public class HiImpl implements HiSEI {
public String sayHi(String s) {
return "Hi " + s;
}
}

NetBeans IDE 4.1 will generate both these files in one go after you define the web service in a wizard.
Whenever you use the IDE to add an operation to the implementation class, the IDE will register it in
the webservices.xml file and generate its declaration in the interface class.
Right-click the WEB-INF node and choose New > File/Folder.
Under Categories, select XML. Under File Types, select XML Document. Click Next.
Name the file jaxrpc-ri, click Next, and click Finish.
jaxrpc-ri.xml opens in the Source Editor.
Replace the content of jaxrpc-ri.xml with the following code:
<?xml version="1.0" encoding="UTF-8"?>
<webServices
xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/dd"
version="1.0"
targetNamespaceBase="http://com.test/wsdl"
typeNamespaceBase="http://com.test/types"
urlPatternBase="/JaxrpcWS">
<endpoint
name="MyHi"
displayName="HelloWorld Service"
description="A simple web service"
interface="org.me.hi.HiSEI"
implementation="org.me.hi.HiImpl"/>
<endpointMapping
endpointName="MyHi"
urlPattern="/hi"/>
</webServices>
NetBeans IDE 4.1 will generate the JSR-109 version of this file when you create
the web service.
The project's context path and relative URL must
correspond to the urlPatternBase and urlPattern fields in the
jaxrpc-ri.xml file. Right-click the project node in the Projects window and choose Properties.
In the Running Project pane, make sure /JaxrpcWS is filled in the Context Path text box
and type /hi in the Relative URL text box. Click OK.
Right-click the JaxrpcWS project node and choose Build Project.
Look in the Files window. The classes and the XML file are copied to the build/web/WEB-INF folder.
The files have been bundled into a WAR file within the dist folder.
This WAR file is not ready for deployment because it does not contain the WSDL file and other
service artefacts that the web service needs for deployment.
You generate these in the next step.
NetBeans IDE 4.1 will perform the next step, the creation of the service artefacts, whenever you
build the project.
In the Files window, add the following target to the Ant build script (build.xml) that the IDE
generated for you when you created the web application. Make sure that you replace each instance of
$JWSDPDIR below with the correct path to your Java Web Services Developer Pack (JWSDP) 1.5 folder.
<target name="create-service-artefacts"
description="Create Service-Side Artefacts">
<taskdef name="wsdeploy" classname="com.sun.xml.rpc.tools.ant.Wsdeploy">
<classpath>
<fileset dir="$JWSDPDIR/jaxrpc/lib">
<include name="**/*.jar"/>
</fileset>
<fileset dir="$JWSDPDIR/jwsdp-shared/lib">
<include name="**/*.jar"/>
</fileset>
<fileset dir="$JWSDPDIR/saaj/lib">
<include name="**/*.jar"/>
</fileset>
</classpath>
</taskdef>
<wsdeploy
outWarFile="dist/JaxrpcWS-generated.war"
inWarFile="dist/JaxrpcWS.war"
verbose="true"
keep="true"
tmpdir="build">
</wsdeploy>
</target>

NetBeans IDE 4.1 will generate this target for you.
Optionally, instead of specifying the JAR files in the Ant build script,
choose Tools > Options, then Building > Ant Settings. Browse to the following
JAR files in your JWSDP distribution and add them to the Additional Classpath:
- jaxrpc-api.jar
- jaxrpc-impl.jar
- jaxrpc-spi.jar
- saaj-api.jar
- saaj-impl.jar
- activation.jar
- mail.jar
For further details on setting your Ant build script's classpath, see
Including Custom Ant Tasks in an Ant Script's Classpath.
In the Files window, right-click the build.xml file and choose
Run Target > create-service-artefacts.
Look in the Files window. The build folder contains a new subfolder
with a name that is generated by the wsdeploy tool when you run the
create-service-artefacts target. In the
generated subfolder's WEB-INF folder is the MyHi.wsdl file and the web service artefacts.
The files have been bundled into the JaxrpcWS-generated WAR file within the dist folder.
This WAR file is now ready for deployment because it contains everything that a JAX-RPC web service
needs.
Delete the web folder in the build folder. Now rename the
folder generated by the wsdeploy tool to "web". In the next step, when you run the project,
the IDE will use the folder that the wsdeploy tool generated, and
not the folder that the IDE created when you built the project.
Note: When you try to rename the folder,
you might find that it is locked, so that
you cannot rename it. If this happens, restart the IDE to unlock the folder.
NetBeans IDE 4.1 will handle this situation more elegantly.
Right-click the project node in the Projects window and choose Run Project.
The web server starts and the application, which includes the web service, is deployed and
displayed in the IDE's default browser, as illustrated below:

Consuming the Web Service
Generate the Client Stubs from the Running Web Service
Expand the Web Pages node, right-click the WEB-INF node and choose New > XML Document.
Name the file config, click Next, and click Finish.
config.xml opens in the Source Editor.
Replace the content of config.xml with the following code:
<?xml version="1.0" encoding="UTF-8"?>
<configuration
xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config">
<wsdl location="http://localhost:8084/JaxrpcWS/hi?WSDL" packageName="org.me.hi"/>
</configuration>
NetBeans IDE 4.1 will generate the JSR-109 version of this file when you create
the web service.
In the Files window, add the following target to the Ant build script (build.xml):
<target name="generate-client-stubs" description="Generate Client-Side Stubs">
<taskdef name="wscompile" classname="com.sun.xml.rpc.tools.ant.Wscompile"/>
<wscompile
gen="true"
base="build/stubs"
classpath="src"
verbose="true"
config="web/WEB-INF/config.xml">
</wscompile>
</target>
NetBeans IDE 4.1 will generate this target for you when you create the web service client.
Create the build/stubs folder that is referred to in the file above
by right-clicking the build folder, and choosing New > File/Folder. Under Categories, select Other.
Under File Types, select Folder. Type stubs in the Folder Name text field.
NetBeans IDE 4.1 will not require you to do this.
Make sure that the web service is still deployed and then
run the generate-client-stubs target. You should get the following output:
In the Files window, you should now see the client stubs in your build/stubs folder:
Note that the org.me.hi package structure is taken from the packageName attribute in the
config.xml file, which you created in step 1 in this section.
Create the Java Application Project from the Client Stubs
Choose File > New Project. Under Categories, select General. Under
Projects, select Java Project from Existing Sources and click Next.
Change the Source Packages Folder to the stubs folder that you created above,
and that now contains the client stubs.
Under Project Name, enter JaxrpcWSC.
Click Finish.
When you are prompted to let the IDE delete the class files, click Ignore.
Those class files are your stubs, and you need them!
The IDE creates the $PROJECTHOME/JaxrpcWSC
folder. The project opens in the IDE. You can view its logical
structure in the Projects window and its file structure in the Files window.
Set the classpath to include the JAX-RPC libraries that you
used for the web service.
Build the project.
Go to the Files window and check the JaxrpcWSC.jar file in the dist folder.
Code the Web Service Client
Create the HiClient.java file in the org.me.hi package and
use the following code:
package org.me.hi;
import javax.xml.rpc.Stub;
public class HiClient {
public static void main(String[] args) {
try {
Stub stub = createProxy();
HiSEI hi = (HiSEI)stub;
System.out.println(hi.sayHi(" Herman!"));
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static Stub createProxy() {
return (Stub)(new MyHi_Impl().getHiSEIPort());
}
}
Deploy the Web Service Client
Right-click the HiWSC project node in the Projects window.
Choose Run Project and then select the main class.
The web service client is deployed.
You should now see the web service's "Hi" and the client's " Herman!" displayed in the Output Window:

|
Related Documents
Quickly create, build, and deploy a simple HelloWeb
web application.
Create a J2EE application consisting of a web application
module that consumes a service exposed by another web application module
or an EJB module.
Transition guide for NetBeans IDE 3.6 users.
A complete list of docs for NetBeans IDE 4.0.
|