Welcome to my Tomcat tutorials website!

martedì 20 gennaio 2009

Tomcat axis tutorial

Axis is essentially a SOAP engine, that is a framework for constructing SOAP processors such as clients, servers, gateways, etc.

Installing Axis on Tomcat

At first download Axis from the Apache project:


Explode the zip folder. Now you need to deploy the Axis engine to Tomcat: Here's how to do it:

Rename the folder axis under AXIS_HOME/webapps to axis.war and copy the folder under "webapps" of your tomcat

Verify that Axis has been deployed correctly:


Publishing Web Services with Axis

Let's say we have a simple class like the following:

package test;

public class HelloWorld {

public String hello(String message) {

return "Invoked with" +message;


How do we go about making this class available via SOAP? There are a couple of answers to that
question, but we'll start with an easy solution.

At first compile the class :

javac -d . HelloWorld.java

Now copy the class under WEB-INF/classes of your axis.war

Ok, now the last step is registering your Web Service so that Axis is aware of it. A Web Service can be registered with a Deployment Descriptor (WSDD) file. A deployment descriptor contains a bunch of things you want to "deploy" into Axis - i.e. make available to the Axis engine.

Here's a simple wsdd file for our WebService:


<service name="HelloWorld" provider="java:RPC">
<parameter name="className" value="test.HelloWorld"/>
<parameter name="allowedMethods" value="*"/>

Pretty simple, really - the outermost element tells the engine that this is a WSDD deployment,and defines the "java" namespace. Then the service element actually defines the service for us. A service is a targeted chain which means it may have any of: a request flow, a provider, and a response flow.

In this case, our provider is "java:RPC", which is built into Axis, and indicates a Java RPC service. We need to tell the RPCProvider that it should instantiate and call the correct class (e.g. test.HelloWorld), and another to tell the engine that any public method on that class may be called via SOAP (that's what the "*" means)

Now save your wsdd file as deploy.wsdd and run the AdminClient which is an Utility to deploy the WebService

java -classpath %AXIS_HOME%/lib/axis.jar;%AXIS_HOME%/lib/commons-discovery-0.2.jar;%AXIS_HOME%/lib/commons-logging-1.0.4.jar;%AXIS_HOME%/lib/saaj.jar;%AXIS_HOME%/lib/jaxrpc.jar org.apache.axis.client.AdminClient deploy.wsdd

If everything was Ok now you should see the WebService deployed on Axis: check the "List" option from the URL http://localhost:8080/axis/

The client

Let's take a look at an example Web Service client that will call the hello method of the HelloWorld Web Service

package test;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import java.net.URL;

public class Client {

public static void main (String args[]) {

try {

String url;


Service service = new Service();

Call call = (Call)service.createCall();

call.setTargetEndpointAddress(new URL(url));

call.setOperationName(new QName("HelloWorld", "hello"));

Object[] params = new Object[1];

params[0] = "Hello Message";

Object result = call.invoke(params);

System.out.println("result is " + result);


catch(Exception e) {





In order to invoke our Web Service we use the Service and Call objects. These are the
standard JAX-RPC objects that are used to store metadata about the service to invoke.

With this we set up our endpoint URL - this is the destination for our SOAP message.

call.setTargetEndpointAddress(new URL(url));

With this we define the operation (method) name of the Web Service.

call.setOperationName(new QName("HelloWorld", "hello"));

Then we actually invoke the desired service, passing in an array of parameters - in this case just one String.

Object result = call.invoke(params);
Obtaining WSDL for deployed services
When you make a service available using Axis, there is typically a unique URL associated with that service. For our service it's http://localhost:8080/axis/services/HelloWorld?wsdl
When you point your browser to that location, Axis will automatically generate a service description for the deployed service, and return it as XML in your browser :

<?xml version="1.0" encoding="UTF-8" ?>
<wsdl:message name="helloRequest">
<wsdl:part name="in0" type="xsd:string" />
<wsdl:message name="helloResponse">
<wsdl:part name="helloReturn" type="xsd:string" />
<wsdl:portType name="HelloWorld">
<wsdl:operation name="hello" parameterOrder="in0">
<wsdl:input message="impl:helloRequest" name="helloRequest" />
<wsdl:output message="impl:helloResponse"
name="helloResponse" />
<wsdl:binding name="HelloWorldSoapBinding" type="impl:HelloWorld">
<wsdlsoap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="hello">
<wsdlsoap:operation soapAction="" />
<wsdl:input name="helloRequest">
namespace="http://test" use="encoded" />
<wsdl:output name="helloResponse">
use="encoded" />
<wsdl:service name="HelloWorldService">
<wsdl:port binding="impl:HelloWorldSoapBinding"
location="http://localhost:8080/axis/services/HelloWorld" />

Generating Java Classes from WSDL

Another approach is to generate Java Classes starting from WSDL contract. You can use the
AdminClient to follow this approach:

java -classpath %AXIS_HOME%/lib/wsdl4j-1.5.1.jar;%AXIS_HOME%/lib/axis.jar;%AXIS_HOME%/lib/commons-discovery-0.2.jar;%AXIS_HOME%/lib/commons-logging-1.0.4.jar;%AXIS_HOME%/lib/saaj.jar;%AXIS_HOME%/lib/jaxrpc.jar org.apache.axis.wsdl.WSDL2Java http://localhost:8080/axis/services/HelloWorld?wsdl

This will generate the following classes under the service namespace which is localhost\axis\services\HelloWorld


Nessun commento:

Posta un commento