This article will show you how to use NetBeans IDE 4.1 to consume a simple web service quickly and easily.
It will demonstrate the web service facilities available in NetBeans IDE 4.1, but will not delve into
too much detail. This will be done in Part 2.
The web service that will be consumed will be a simple quotation service, which returns a 'Thought for the Day'
whenever its operation is invoked. It is intentionally a simple example, although you can, of course, extend it.
Part 2 will provide a more useful example, which will also be more complex and robust.
Note: An application using web services is as strong as its weakest web service. Should a web service be taken
out of circulation, or should it not be functioning for any reason whatsoever, the entire application on which it is
based falls apart. The same is true for this tutorial and the web service on which it is based. There is no guarantee
that the Quotation service consumed in this tutorial will always be available. However, even if it isn't, the steps
below should provide enough general information for the consumption of a service other than the demonstrated Quotation service.
After you have installed and set everything up, the example can be built in 10 minutes.
Getting Started
Installing the Software
Before you begin, you need to have the following software installed on your computer:
Sun Java System (SJS) Application Server Platform Edition 8 2005Q1 (download)
Java Standard Development Kit (JDKTM) version
1.4.2 (download)
or 5.0 (download)
Registering the Sun Java System Application Server
Before you can create, compile, and deploy web services, you have to register a local instance
of the SJS Application Server. If you installed the NetBeans IDE 4.1/SJS Application
Server bundle, the local application server is registered automatically.
Choose Tools > Server Manager from the main window.
Click Add Server. Select Sun Java Systems Application Server 8.1 and give
a name to the instance. Then click Next.
Specify the server information, the location of the local instance of
the application server, and the domain to which you want to deploy.
If you are behind a firewall, you need to set a proxy host and port number on the SJS Application Server.
See the first part of Troubleshooting for help.
Consuming Your First Web Service
To consume a web service, you need to create a web service client.
For the creation of web service clients, NetBeans IDE 4.1 provides a client creation facility -- the Web Service Client
wizard that generates code for looking up a web service. It also provides facilities for developing the created web service client
-- a work area consisting of nodes in the Projects window and a built-in client for testing and analyzing web services.
These facilities are part of the standard NetBeans IDE installation, they're available straight out of the box and no plug-ins
are needed.
Creating the Client
Choose File > New Project (Ctrl-Shift-N). Under Categories, choose Web. Under Projects,
choose Web Application. Click Next. Name the project MyApplication and make sure
that you specify the Sun Application Server as your target server. Click Finish.
In the Projects window, right-click the MyApplication project node and
choose New > File/Folder. In the New File wizard choose Web Service > Web Service Client. The Web Service Client wizard appears. This
wizard is used for retrieving the WSDL file on which your client will be based and for generating the files and code
snippets that enable the client to connect to the web service. (In the illustration below, the wizard is filled with
the values you specify in the next step.)
In the
Web Service Client wizard you specify the WSDL file that you want to
consume.
In this case, we'll consume a service that retrieves quotations. In the WSDL URL field above, enter the following URL:
Click Retrieve WSDL to download it. If you are behind a firewall, you might need
to set a proxy -- otherwise the WSDL file cannot be downloaded. If so, click Proxy Settings and set
your proxy host and port number. Next, enter a package
name. The package name specifies the package to which the client files will be generated.
Click Finish. In the Projects window, within the Web Service References node, you should see the following:
The Projects window shows that a web service called 'Quotes' has made the operation 'getQuote'
available to your application. In the Files window, you can see the files generated by the Web Service Client wizard in the project's
build folder. More time will be spent on this view in Part 2.
Testing the Web Service
In the Projects window, within the Web Service References node, double-click the 'getQuote' operation.
The Test Web Service Operation dialog box appears. If the operation required arguments, the upper part of this
dialog box would let you specify them. (See Part 2 for details.) However, the getQuote operation requires no arguments.
Click on the Submit button. When you do this, the web service is contacted and the operation is invoked.
Wait for the quotation to be retrieved (it should not take long), click on the Value at the bottom of the dialog box, and you should then
see something similar to the following:
If you see something similar to the results displayed in the above illustration, your test has succeeded. The web service
is up and running and has returned a quotation. If a quotation is not returned and you are behind a firewall,
see the second part of Troubleshooting for help.
Developing the Client
Right-click the project node in the Projects window,
choose New > File/Folder and then choose Web > Servlet. Click
Next. Name the servlet MyApplicationServlet and type mypackage.mysubpackage in the Package drop-down.
Click Next. Note that the URL mapping for this servlet is /MyApplicationServlet and click Finish. The servlet opens in the Source Editor.
Put your cursor inside the Source Editor, inside the processRequest method, right-click, and choose Web Service
Client Resources > Call Web Service Operation. Click the getQuote
operation in the "Select Operation to Invoke" dialog box and then click
OK.
Bits of code have now been added to the servlet. Scroll to the bottom,
and you see code that connects the client to the web service (much
of the code is underlined in red -- ignore that, it's a bug that has no
effect on the building and running of the application).
At the top of
the processRequest method you see a snippet of code that
invokes the web service. Cut and paste it into the body tags of the processRequest
method, and delete the lines that comment out the code (put your cursor
in the line and press Ctrl-E to delete the whole line). The method now
looks as follows:
Add String myReturnedQuotation = in front of the first line in the try/catch block
and then type out.println(myReturnedQuotation); in the line below, so that the try/catch block is now like this:
try {
String myReturnedQuotation = getQuotesSoap().getQuote(/* TODO enter operation arguments */);
out.println(myReturnedQuotation);
} catch(java.rmi.RemoteException ex) {
// TODO handle remote exception
}
Save the servlet. Remember that you can ignore the red underlinings in this case -- it's a known bug that has no
effect on the building and running of the application.
Deploying the Client
Right-click the project node and choose Run Project. After a while, the application should deploy
and display a blank JSP page in the IDE's default browser.
In the URL at the top of the page, type the servlet's URL mapping (MyApplicationServlet).
Alternatively, before deploying the client, you can also specify the URL mapping in the Run panel of
the project's Project Properties dialog box, which you access by right-clicking the project
node and choosing Properties.
Now you should see something like this:
If you are behind a firewall and have not set your proxy host and port number on the
Sun Java System Application Server, an error will be displayed in the browser instead of a
quotation from the web service. See Preparing to Deploy
for help.
That's it. You're done. Without very much coding, you created a client and consumed a web service. In Part 2,
you will interact more closely with the web service facilities provided by NetBeans IDE 4.1 and you will acquire
a fuller understanding of the interaction between a client and the web services it consumes.
Troubleshooting
Since the IDE provides most of the code you need for communicating with the web service,
the only problems you should encounter are errors in your own code. In addition, though, you
might encounter a few problems if you are behind a firewall. The troubleshooting tips that follow
attempt to resolve these problems
If you deploy the application without setting your proxy host and port
correctly, you will see this in your browser:
To fix this problem, do the following:
In the Runtime window, make sure that the Sun Java System Application Server is running.
If it is running, the text [running] is displayed to the right of the server's name.
Right-click the JVMs node and choose Properties.
Click the ellipsis to the right of the JVMOptions property.
Add the following two properties to the list:
-Dhttp.proxyHost
-Dhttp.proxyPort
Make sure that the values you set for these properties are appropriate to your environment.
The dialog box should now look similar to the following:
If you test the application in the IDE without setting your proxy host and port
correctly, this error will be displayed in the IDE:
To fix this problem, do the following:
Choose Tools > Options.
In the Options window, set the proxy host and port number.
Applying What You Have Learned
Add error handling code to the servlet.
Rewrite the client so that the user can interact with the data returned from the web service.
For inspiration, see the client implementation on the CDYNE site (click here).
Next Steps
For more information about using NetBeans IDE 4.1, see the following resources:
To send comments and suggestions, get support, and keep informed on the latest
developments on the NetBeans IDE J2EE development features, join the
mailing list
.