Tutorials : Web Services with Axis :

Looking at WSDL

A Web Service can be described in XML style using WSDL = Web Service Description Language. I'll not go into details about this format (see the Resources section for links to more information), but Axis has a nice feature that will produce a WSDL file for you. In your XML-aware browser (e.g. Internet Explorer) simply append "?wsdl" to the service URL, hit Enter, and you'll see the wsdl xml file. I have edited it slightly to ease your reading. A couple of lines from the file should look familiar to you:

The Service URL:

<soap:address location="http://localhost:8080/axis/NHLService.jws"/>

The input parameters:

<message name="getCurrentPositionRequest">
   <part name="division" type="xsd:string"/>
   <part name="team" type="xsd:string"/>
</message>

The output parameter:

<message name="getCurrentPositionResponse">
   <part name="return" type="xsd:string"/>
</message>

The name of the service method (in bold), and the input and output parameters (the names in italic can be found above):

<portType name="NHLService">
   <operation name="getCurrentPosition" parameterOrder="division team">
     <input message="getCurrentPositionRequest"/>
     <output message="getCurrentPositionResponse"/>
   </operation>
</portType>

Having a WSDL-file it's a simple matter to code a Java-client that will access the Web Service. In the NHLServiceClient program above we simply have to replace

  • the Service URL
  • the method name (and maybe a related Namespace value)
  • and of course the input and output parameters

So let's go looking for some web services on the Internet and then code a couple of clients to access them.

Web Services on the Internet

On http://www.xmethods.com/ you'll find plenty of Web Services. I'll show you how to read the descriptions of these services and adjust the Java-client to access them.

Example 1: California highway conditions

Locate this line on the xmethods page:

xmethods.net / RPC / California Traffic Conditions / California highway conditions. / Apache SOAP

Now click on the hyperlink and you'll see a new page that contains these links:

Clicking on the third link will show the WSDL file for the California traffic service. The first link will show a series of pages again giving details of the WSDL information. The second link gives us just what we need to code our Java client:

  • the Service URL: http://services.xmethods.net:80/soap/servlet/rpcrouter
  • the method name: getTraffic and method namespace: urn:xmethods-CATraffic
  • and of course the input and output parameters: two strings

The method namespace will have to be used to qualify the method name. The java client program now becomes:

package hansen.playground;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.rpc.namespace.QName;

import java.net.*;

public class Traffic {
   public static void main(String [] args) throws Exception {
       
       Service service = new Service();
       Call call = (Call)service.createCall();

       String endpoint = "http://services.xmethods.net:80/soap/servlet/rpcrouter";
       call.setTargetEndpointAddress(new URL(endpoint));
       call.setOperationName( new QName("urn:xmethods-CATraffic", "getTraffic") );

       String hwnum = args[0]; // get a highway number
       String answer = 
         (String)call.invoke(new Object [] {new String(hwnum)});
        
       System.out.println("Got result : " + answer);
   }
}

The important parameters are shown in bold. The only new thing is the method namespace. If we run the program with "101" as input we get this result:

reported as of Monday, April 8, 2002 at 12:12 .
Slow for the Cone Zone

US 101
    [LOS ANGELES & VENTURA CO.'S]
    NO TRAFFIC RESTRICTIONS ARE REPORTED FOR THIS AREA.

    [CENTRAL COAST]
    NO TRAFFIC RESTRICTIONS ARE REPORTED FOR THIS AREA.
 
    [SAN FRANCISCO BAY AREA]
    NO TRAFFIC RESTRICTIONS ARE REPORTED FOR THIS AREA.
 
    [NORTHWEST CALIFORNIA]
    NO TRAFFIC RESTRICTIONS ARE REPORTED FOR THIS AREA.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.