Web Services with Axis
by Keld H. Hansen
Web Services - only for nerds?
This article is about web services. It's meant as an introduction for those
of you who think that web services are only for computer nerds. They're not. If
you think otherwise it might be because of the many difficult-to-read articles
that have tried to address the subject, often in very technical terms. Web
services are nothing but two programs interchanging data on the Internet--or an
intranet--in the XML-based format called SOAP. Ooops… one 3-letter and one
4-letter acronym already, and I was trying to show the simplicity of web
services. Let me try another angle.
If you're a Java programmer (and you probably are if you're surfing on
javaboutique.com) and have just written a nice program that you'd like to expose
as a web service on the Internet, then how do you do that? Well I'll show
you.
Deploying a Web Service
Let's assume that your nice little program does this: the user gives the name
of one of the teams in the U.S. National Hockey League, and the service returns
the team's current position. Since this article concentrates on the techniques
behind web services, we'll not dig into where to get the real information about
the teams positions. So the next Java program simply gets the positions from a
Java HashMap containing some static data:
import java.util.*;
public class NHLService {
HashMap standings = new HashMap();
public NHLService() {
// NHL - part of the standings as per 04/07/2002
standings.put("atlantic/philadelphia", "1");
standings.put("atlantic/ny islanders", "2");
standings.put("atlantic/new jersey", "3");
standings.put("central/detroit", "1");
standings.put("central/chicago", "2");
standings.put("central/st.louis", "3");
}
public String getCurrentPosition(String division, String team) {
String p = (String)standings.get(division + '/' + team);
return (p == null) ? "Team not found" : p;
}
}
As you can see, this is just a simple Bean, which could be used in any Java
program. Nevertheless it's a simple matter to make its "getCurrentPosition"
method available as a web service. The key to this could be the "Axis" framework from Apache. What Axis is
and how Axis is installed on your computer we'll return to later. Right now I'll
show you the few steps needed to create and use the "getCurrentPosition" web
service.
First you copy the NHLService.java file into the Axis directory (I'll promise
you I'll return to what this is!) on your web server. Then you rename the file
to NHLService.jws (JWS stands for Java Web Service). Note that we don't compile
the java program. And then . . . well . . . that's all . . . the service is
deployed and available. There's nothing more to it.
Coding a Web Service Client
To prove this we make a small client program with a main method that uses the
web service:
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 NHLServiceClient {
public static void main(String [] args) throws Exception {
Service service = new Service();
Call call = (Call)service.createCall();
String endpoint = "http://localhost:8080/axis/NHLService.jws";
call.setTargetEndpointAddress(new URL(endpoint));
call.setOperationName(new QName("getCurrentPosition"));
String division = args[0];
String team = args[1];
String position =
(String)call.invoke(new Object [] {new String(division), new String(team)});
System.out.println("Got result : " + position);
}
}
This might seem complicated at first glance, but actually it's not. What we
need to specify is the URL of the jws-file and the name of the method to invoke.
Of course we also need to give the two strings with the division and team names.
The convention is that they must be given as an Object array. What is returned
from the "invoke" call is simply the result from "getCurrentPosition". If we run
the main method with the arguments "central detroit" we'll see written out "Got
result : 1", which is the correct standing for this team.
Part of the magic in this is that the NHLService.jws file is compiled
automatically by Axis the first time it's referenced. You can locate the
compiled class file in the Axis directory.
The Axis Servlet
You might be tempted to enter the URL from the above program into your
browser to see what happens. If you do you'll see this:
This answer actually tells us what you might already have guessed: we're not
talking with our own NHLService class, we're talking to "Axis", and since Axis
doesn't expect input from a browser, it'll give you this somewhat cryptic
answer. Since we're curious we might look into the Axis web.xml configuration
file where we find this:
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>*.jws</url-pattern>
</servlet-mapping>
No surprise if you're familiar with servlets: it simply says that any
references to files with extension "jws" will invoke the "AxisServlet" servlet.
This was the first part of the exercise: to show you that defining, deploying
and using a web services doesn't have to be rocket science. But I won't try to
hide that what we have seen up to now is only the tip of the iceberg. The people
behind the Axis project have very cleverly hidden a lot of the technical stuff
that makes a web service run. But using jws-files will only solve the more
simple cases, so we will try to dig a little deeper into Axis and web services
to see what's going on behind the facade.
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.