Web Service with AXIS

This article shows how to use AXIS from Apache to host Web Services using Tomcat5.

What is Web Service?

Web service is a collection of methods that can be accessed over internet using SOAP (Simple Object Access Protocol). SOAP itself is making use of HTTP and XML to encode and trasfer data.

Web services are interoperable , which means a Web service created in Java can be accessed from .NET and vice-versa.

What is AXIS?

AXIS is SOAP engine which works on server and client. It allows Java classes to be deployed as Web services so that they can be accessed from anywhere.

AXIS stands for Apache Extensible Interaction System. It is an open-source project and implements standard JAX-RPC API of Java.

How to install Axis?

Axis must be downloaded from axis.apache.org. It comes as a simple .zip file axis-bin-1_3.zip Extract this zip file to D drive will result in a folder with the name d:\axis-1_3.

Copy axis directory of d:\axis-1_3\webapps into webapps directory of Tomcat5.

Start Tomcat and test whether the Axis web application is running using http://localhost:8080/axis . Please change port number 8080 with port number that you use for Tomcat in your system.

Test whether Axis has access to all required libraries by using http://localhost:8080/axis/happyaxis.jsp. If any core libraries are missing, make sure you copy them to either WEB-INF/lib directory of Axis or common\lib directory of Tomcat.

Creating and comsuming a simple Web service

The following procedure explains how to create a invoke a simple web service, which has a single method sayHello().
  1. Create the following class and place it in axis directory under the name Hello.jws.
    public class Hello
    {
    
     public String sayHello( String name)
     {
       return  "Hello," + name;
     }
    }
    
  2. Test the web service by giving the url http://localhost:8080/axis/Hello.jws.
    You must see the message saying that there is a web service installed. Click on wsdl hyperlink to see WSDL for the service.
  3. Once service is deployed successfully then run the client by first setting the CLASSPATH and PATH as follows:
    set AXIS_HOME=d:\axis-1_3
    set AXIS_LIB=%AXIS_HOME%\lib
    set AXISCLASSPATH=.;%AXIS_LIB%\axis.jar;%AXIS_LIB%\commons-discovery-0.2.jar;%AXIS_LIB%\commons-logging-1.0.4.jar;%AXIS_LIB%\jaxrpc.jar;%AXIS_LIB%\saaj.jar;%AXIS_LIB%\log4j-1.2.8.jar;%AXIS_LIB%\xml-apis.jar;%AXIS_LIB%\xercesImpl.jar
    
    set classpaht=.;%classpath%;%AXISCLASSPATH%
    
    path c:\jdk1.5.0\bin
    
    
    You have to copy xercesimpl.jar and xml-apis.xml or something similar to it must be copied into lib directory of d:\axis-1_3.

    HelloClient.java is the client program to access web serice.

    
    import org.apache.axis.AxisFault;
    import org.apache.axis.client.Call;
    import org.apache.axis.client.Service;
    import org.apache.axis.encoding.XMLType;
    import org.apache.axis.utils.Options;
    
    import javax.xml.namespace.QName;
    import javax.xml.rpc.ParameterMode;
    import java.net.URL;
    
    public class HelloClient
    {
      public static void main(String args[])  throws Exception
      {
    
        Service  service = new Service();
        Call     call    = (Call) service.createCall();
        call.setTargetEndpointAddress( "http://localhost:8080/axis/Hello.jws");
        call.setOperationName("sayHello");
        Object ret = call.invoke( new Object[] {"Srikanth"} );
        String res  = (String) ret;
        System.out.println(res);
       }
    }
    
    
  4. Then complie and run the following client.

    javac HelloClient.java

    java HelloClient

    You must see message saying Hello,Srikanth. If you get any warning regarding Log4j, ignore them.

    Installing a Web service by just copying .jws file into axis directory is called as drop-in deployment.

Deploying Web Serice using Custom deployment

The following example demonstrates how to deploy a web serice using a .class of Java. We want to expose a few methods (or all) of a Java class as web methods in web service. This can be done by the following procedure:
  1. Create Hello.java as follows:
    public class Hello
    {
    
     public String sayHello( String name)
     {
       return  "Hello," + name;
     }
    }
    
  2. Compile Hello.java and copy Hello.class into WEB-INF/classes directory of axis application in Tomcat.
  3. Create Hello.wsdd , which contains details of the sercice as follows:
    <deployment name="test" xmlns="http://xml.apache.org/axis/wsdd/"
        xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
    
      <service name="hello" provider="java:RPC">
        <parameter name="className" value="Hello"/>
        <parameter name="allowedMethods" value="*"/>
      </service>
    </deployment>
    
  4. Run AdminClient to deploy service in Hello.wsdd as follows:

    java org.apache.axis.client.AdminClient -lhttp://localhost:8080/axis/services/AdminService deploy.wsdd

  5. Test web service by using http://localhost:8080/axis/services/hello. You must see a page saying this is a web service.
  6. Create the HelloClient.java as follows:
    import org.apache.axis.AxisFault;
    import org.apache.axis.client.Call;
    import org.apache.axis.client.Service;
    import org.apache.axis.encoding.XMLType;
    import org.apache.axis.utils.Options;
    
    import javax.xml.namespace.QName;
    import javax.xml.rpc.ParameterMode;
    import java.net.URL;
    
    public class HelloClient
    {
     public static void main(String args[])  throws Exception
     {
       Service  service = new Service();
       Call     call    = (Call) service.createCall();
       call.setTargetEndpointAddress( "http://localhost:8080/axis/services/hello");
       call.setOperationName("sayHello");
    
       Object ret = call.invoke( new Object[] {"Srikanth"} );
       String res  = (String) ret;
       System.out.println(res);
      }
    }
    
  7. Compile and run HelloClient to get call sayHello() method. The return value will be Hello,Srikanth .

Conclusion

Axis really made the process of creating and deploying web serice in Java very simple. All that you have to create a web service is Tomcat5.x and Axis and a Java compiler (J2SE 5.0).

For further information, refer to axis.apache.org. Also remember axis comes with full documenation in docs directory.

Click Currency Converter Example to see how to create a currency conveter web service and corresponding client.

Keep Learning,

P.Srikanth