In this tutorial we take a fresh look at building Web services using the latest release of NetBeans, an open source Java GUI for Windows, Linux and OS X. This tutorial is designed for Web application developers of any level, whether you have Java experience or not. It should take you less than an hour from start to finish, which is a testament to the maturity of Java development tools such as NetBeans. So let's dive in.
Installation and configuration
To get up and running with Java Web services, you need the latest version of the Java 2 SDK and the Java Web Services Developer Pack. To complete this tutorial you will also need NetBeans 4.1 and the Sun Application Server. If possible, it is best to install these in the following order:
- Java 2 Standard Development Kit 1.4.2;
- Java Web Services Developer Pack 1.5;
- Sun Java Systems Application Server Platform Edition 8 2005Q1;
- NetBeans 4.1 RC2 or later.
Once you have all the above software installed, you will need to configure NetBeans to use the Sun Application Server instead of the bundled TomCat server by default. To do this, go to Tools -> Server Manager and click Add Server. Here you need to locate the server's installation directory and supply the login you created during the setup process.
Creating the Web service
From the NetBeans File menu select New Project and choose Web Application from the dialog box before clicking Next. In the New Web Application window, specify a name and location for the project and click Finish. NetBeans will now create an empty Web application with a default JSP page that opens in the editor pane. Right click the project node in the Projects pane and select New -> Web Service from the menu. Here, specify a name and package namespace for the Web service.
|
| Figure 1: Adding a handler |
package demo.maths.ws;
public class SimpleMathsWSImpl implements
SimpleMathsWSSEI {
public float squared(float num) {
// TODO implement operation
return 0;
}
}
The next step is to implement the function for the operation. Our squared routine simply returns the input parameter value multiplied by itself. Hence the final code for the operation looks like this:
public float squared(float num) {
return num * num;
}
|
| Figure 2: Starting a Web service |
We now need to make the Web service the default handler for the application, and this is done from the project properties window. Right click the project in the Projects pane and select properties. Next, select the run node and enter the name of the Web service in the Relative URL field. Our Web service is called SimpleMathsWS, so we used /SimpleMathsWS as the default URL for the project. It is now possible to build and deploy the project so the Web service can be registered and consumed. Simply right click the project and choose Run from the menu. The default browser will open and show the following error message.
Invalid wsdl request
http://localhost:8080/SimpleMathsWS/SimpleMathsWS for web service SimpleMathsWS
This means our Web service is working, but a valid request was not made. The Web service is now ready to be consumed by a Web service client, but first, let's add a SOAP message handler that logs the activity of the Web service.
|
| Figure 3: Browser output |
Do you need help with Java, C, or C++? 




