«^»
9.2. A better way: six steps to providing an Axis Web Service

In my view, the best way of deploying an Axis Web Service is to use the following six steps. It is one of the ways described in the Axis User's Guide.

9.2.1. Step 1: Provide a Java interface or class and compile it

The first step is to provide and then compile an interface declaration or a class declaration giving details of the methods that you wish to make available as a Web Service.

So we could provide the following interface declaration:

0215: package Pack;                                                // Convert.java
0216: public interface Convert {
0217:    public double toFahrenheit(double pCentigrade);
0218: }
storing it in the file Convert.java in some subdirectory called Pack. We could then compile it:
javac Pack\Convert.java
This produces the file Convert.class in the subdirectory Pack.

However, if we want our choice of the names of the parameters of methods to prevail, it is better to provide a class declaration and compile it with javac's debug option (-g). The class declaration could consist of method declarations with nearly empty bodies. For example, we could store:

0219: package Pack;                                                // Convert.java
0220: public class Convert {
0221:    public double toFahrenheit(double pCentigrade) {
0222:       return 42;
0223:    }
0224: }
in the file Convert.java in the subdirectory Pack and then compile it using:
javac -g Pack\Convert.java
to produce the file Convert.class in the subdirectory Pack.

9.2.2. Step 2: Use the Java2WSDL program to create a WSDL file

Having produced a .class file, the Java2WSDL program can be used to create a WSDL file. Once again, this program is only accessible if the very long classpath that was given earlier has been set up.

As well as indicating the name of the class (Pack.Convert) and the name of the WSDL file (Convert.wsdl), the command line that is used to run the Java2WSDL program also needs to mention the namespace to be used in the WSDL file and the URL to be used as the endpoint of the Web Service. Here is a typical use of the program:

java org.apache.axis.wsdl.Java2WSDL -o Convert.wsdl ^
      -l"http://localhost:8080/axis/services/Convert" ^
      -n "urn:Convert" -p"Pack" "urn:Convert" Pack.Convert

The Java2WSDL program will ensure that the appropriate message, portType, binding and service elements are included in the WSDL file. If the Java interface/class refers to other interfaces/classes (that we have written), the WSDL file will also contain XML that can be used to represent those types. The Axis User's Guide points out that the Java2WSDL program supports bean classes, arrays and holder classes.

9.2.3. Step 3: Use the WSDL2Java program to create the files for a Web Service

In this step, the WSDL2Java program is used again. Earlier we used it to create proxy files (aka stub files) for a client. It can also be used to create files needed to support the code of a Web Service.

Here is a typical use of the WSDL2Java program for this purpose:

java org.apache.axis.wsdl.WSDL2Java -o . -s -S true -Nurn:Convert Pack Convert.wsdl

When the WSDL2Java program is used with these options, it will examine the WSDL file (that is named as the final parameter) and generate eight or more files. In our example, these files will be created in the Pack subdirectory. There are three kinds of files:

As we are currently only interested in providing a Web Service, the last three files will be ignored.

9.2.4. Step 4: Alter the ConvertSoapBindingImpl.java file

One of the files created by the WSDL2Java program is a file containing an interface declaration that declares the methods of the Web Service. For example, given the above command line it would produce a file called Convert.java that contains:

0225: package Pack;                                                // Convert.java
0226: public interface Convert extends java.rmi.Remote {
0227:    public double toFahrenheit(double pCentigrade) throws java.rmi.RemoteException;
0228: }

It also produces a stab at an implementation of the Web Service in the file ConvertSoapBindingImpl.java:

0229: package Pack;                                 // ConvertSoapBindingImpl.java
0230: public class ConvertSoapBindingImpl implements Pack.Convert {
0231:    public double toFahrenheit(double pCentigrade) throws java.rmi.RemoteException {
0232:       return -3;
0233:    }
0234: }
This file needs to be altered. In our example, the:
0232:       return -3;
needs to be replaced by:
0235:       return 32 + pCentigrade*9/5;

9.2.5. Step 5: Compile the files of the Web Service

The files of the Web Service need to be compiled and put into their correct places. I do this using commands like:

0236: cd Pack
0237: javac *.java
0238: mkdir "%CATALINA_HOME%\webapps\axis\WEB-INF\classes\Pack"
0239: copy Convert.class                    "%CATALINA_HOME%\webapps\axis\WEB-INF\classes\Pack"
0240: copy ConvertSoapBindingImpl.class     "%CATALINA_HOME%\webapps\axis\WEB-INF\classes\Pack"
0241: copy ConvertSoapBindingSkeleton.class "%CATALINA_HOME%\webapps\axis\WEB-INF\classes\Pack"
0242: cd ..
0243: dir "%CATALINA_HOME%\webapps\axis\WEB-INF\classes\Pack"

9.2.6. Step 6: Deploy the Web Service

One of the files resulting from running the WSDL2Java program is a file called deploy.wsdd. In our example, the WSDL2Java program produces a deploy.wsdd file containing:

0244: <deployment
0245:       xmlns="http://xml.apache.org/axis/wsdd/"
0246:       xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
0247:    <service name="Convert" provider="java:RPC" style="rpc" use="encoded">
0248:       <parameter name="wsdlTargetNamespace" value="urn:Convert"/>
0249:       <parameter name="wsdlServiceElement" value="ConvertService"/>
0250:       <parameter name="wsdlServicePort" value="Convert"/>
0251:       <parameter name="className" value="Pack.ConvertSoapBindingSkeleton"/>
0252:       <parameter name="wsdlPortType" value="Convert"/>
0253:       <parameter name="allowedMethods" value="*"/>
0254:    </service>
0255: </deployment>

Essentially this WSDD (Web Services Deployment Descriptor) file is describing:

Once this file has been created (by the WSDL2Java program), we can use the AdminClient program to get the Web Service deployed.

Here is a typical use of the AdminClient program:

java org.apache.axis.client.AdminClient Pack\deploy.wsdd

Note: you can check what Web Services are deployed either by executing the following command in a Command Prompt window:

java org.apache.axis.client.AdminClient list
or by visiting the WWW page at:
http://localhost:8080/axis/servlet/AxisServlet

Note: the following command can be used to undeploy a Web Service:

java org.apache.axis.client.AdminClient Pack\undeploy.wsdd

9.2.7. Accessing the Web Service

Once the AdminClient program has been used to deploy a Web Service, the Web Service can be accessed by client programs in the usual way.