YOU ARE HERE: Home > Tech > Web Services > Article

Programming Java Web Services (III)
By John Henry Xu This article was not rated yet.
 
Printer Version Printer Friendly | Add As Favorite | Link to Article

About the Author

Dr. John Xu is a seasoned system architect and manager. He was chief architect for large distributed portals. He also developed search engine and Java forums. He can be reached by email xixu@yahoo.com.

In last article, I have demonstrated the steps of setting up Axis and enabling web services in Tomcat. We also learned how to write a simple web service using Java. In production environment, it is recommended that complicated web services, including their dependent classes, should be written in java classes and deployed into web application servers in packages.

The previous approach, writing the logic in a .jws file, is an easy way and it is appropriate to write simple web services that way. The problem was that JWS approach makes java web services global objects. Writing java classes for web services, on the other hand, can give you freedom of using design patterns and OO designs for your web services.

This article assumes readers have completed Axis and Tomcat installation. If you did know how to install the above-mentioned software, you need read my article "Programming Java Web Service" on http://www.usanalyst.com to learn the details of Axis and Tomcat installations and set up the servers.

Writing Java Services in Axis

We will follow simple steps to design our web service.

Step 1. Define web service interface

To write a java service, we define the service interface first.

We write an interface file as below

List 1. MathOperation.java


package com.usanalyst.axis;
public interface MathOperation {
public double add(double x, double y);
public double subtract(double x, double y);
public double multiply(double x, double y);
}

To implement this interface, we have a java class

List 2. MathOperationImpl.java


package com.usanalyst.axis;
public class MathOperationImpl implements MathOperation {
public double add(double x, double y) {
return x+y;
}

public double subtract(double x, double y) {
return x-y;
}
public double multiply(double x, double y) {
return x*y;
}
}

Explanation

MathOperationImpl.java implements the interface defined in MathOperation.java

We need compile our java files and make class files.

To do this we use ant to build classes. We have written a simple build file compile.xml in previous article "Programming Java Web Services(II)". And we will reuse it. For your convenience, I write it as List 3

List 3. compile.xml

<project name="MathOperation" default="compile" basedir=".">
<property name="build.home" value="${basedir}"/>
<property name="src.home" value="${build.home}"/>
<target name="compile">
<javac srcdir="${src.home}" destdir="${build.home}" debug="true" deprecation="true">
<classpath>
<pathelement location="D:/j2sdk1.4.2_07/lib" />
<pathelement location="." />
<fileset dir="D:/axis-1_2/lib">
<include name="*.jar"/>
</fileset>
<fileset dir="D:/j2sdk1.4.2_07/jre/lib/endorsed">
<include name="*.jar"/>
</fileset>
</classpath>
</javac>
</target>
<target name="clean">
<delete>
<fileset dir="${basedir}" includes="**/*.class"/>
</delete>
</target>
</project>

Type command

ant -buildfile compile.xml

We will build the classes files.


Step 2. Generate the WSDL File For MathOperation Interface

In the step 2, we use Java2WSDL to generate a WSDL file according to specification of MathOperation interface.

Java2WSDL provides some options when one generates WSDL files. Here are some options:


1. -o directs WSDL output location.
2. -l Intended for web service URL
3. -n is for target namespace in WSDL
4. -p is package information
5. The class with full package information

We can run Java2WDSL and generate corresponding a WSDL file.

java org.apache.axis.wsdl.Java2WSDL -o simplemath.wsdl -l"http://localhost:8080/axis/services/simplemath" -n http://simplemath.usanalyst.com -p"com.usanalyst.axis" http://simplemath.usanalyst.com com.usanalyst.axis.MathOperation

The WSDL file that has been generated is simplemath.wsdl.

Step 3. Generate stub and wrapper codes from WSDL

We use WSDL2Java to generate stub classes and wrapper classes from the WSDL file simplemath.wsdl.

WSDL2Java needs taking option parameters such as


1. -o determines classes output directory.
2. -s generates server side codes for server-side bindings
3. -S determines if server-side skeleton code should be generated. Default is false.
4. -N or -NStoPkg is for namespace and package mapping in generated classes.

In a command console, we type

java org.apache.axis.wsdl.WSDL2Java -o . -s --NStoPkg http://simplemath.usanalyst.com=com.usanalyst.axis simplemath.wsdl

Note com.usanalyst.axis is used for package, and codes are put into directory com/usanalyst/axis.

In the directory com/usanalyst/axis we can find following files:


1. For server deployment
deploy.wsdd
undeploy.wsdd

2. For server side codes
MathOperation.java
SimplemathSoapBindingImpl.java

3. For client stub classes
MathOperationService.java
MathOperationServiceLocator.java
SimplemathSoapBindingStub.java


The SimplemathSoapBindingImpl.java is the implementation file that is generated by machine. We need use our implementation class to calculate mathematic operations.

We add codes in this file, and we highlight the code we added:

List 4. SimplemathSoapBindingImpl.java


package com.usanalyst.axis;

public class SimplemathSoapBindingImpl implements com.usanalyst.axis.MathOperation{

MathOperationImpl moi=new MathOperationImpl();

public double add(double in0, double in1) throws java.rmi.RemoteException {
return moi.add(in0,in1);
}

public double subtract(double in0, double in1) throws java.rmi.RemoteException {
return moi.subtract(in0,in1);
}

public double multiply(double in0, double in1) throws java.rmi.RemoteException {
return moi. multiply(in0,in1);
}

}


As before, we reuse our previous ant build file compile.xml to build the application.

Type command to clean classes files in a command console

ant -buildfile compile.xml clean

And build classe with the command

ant -buildfile compile.xml

Below is the screenshot I have when I compile sources to java classes.


Step 4. Deploy service code to web application server

We want all classes files in a Jar file so we can put it axis directory of Tomcat server.

Type in a command console


jar cvf simplemath.jar com/usanalyst/axis/*.class

We will get the simplemath.jar file in output folder.

Alternatively, you can use ant deploy the Jar file. To understand how to write a deployment build file, you can refer to my paper "Effective Ant Build For Developers" on http://www.usanalyst.com

Copy simplemath.jar to %TOMCAT_HOME%/webapps/axis/WEB-INF/lib.

Start Tomcat web application sever.

And we deploy this web service using Axis AdminClient utility by typing following command in a windows command console:

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

You should see

Done processing

which means the service has been deployed successfully.

So we have successfully written our web service in 4 easy steps.


Testing Java Services in Axis

Now we can write a client application to test our application.

First, in a web browser, open

http://localhost:8080/axis/services/simplemath

We can see the screenshot

Then we write a client application

List 5. TestMath.java

import com.usanalyst.axis.*;

class TestMath {

public static void main(String args[]) throws Exception {
MathOperationService mos=new MathOperationServiceLocator();
MathOperation mo=mos.getsimplemath();
System.out.println("add(2.0,3.6)="+mo.add(2.0,3.6));
System.out.println("subtract(6.4,3.6)="+mo.subtract(6.4,3.6));
System.out.println("multiply(2.0,3.6)="+mo.multiply(2.0,3.6));
return;
}
}

After run

ant -buildfile compile.xml

And

java TestMath

In a command console, we can see the results


add(2.0,3.6)=5.6
subtract(6.4,3.6)=2.8
multiply(2.0,3.6)=7.2

Our math operation web service works.

For details of writing client application to access web services, readers can refer to my previous article "Programming Java Web Services (II)" also on http://www.usanalyst.com.


Was this article helpful to you?yesno

Related Publications
 
Axis Samples Step by Step Tutorials -- Echo
Axis TCP Monitor For SOAP Messages
Programming Java Web Services (III)
Programming Java Web Services (II)
Programming Java Web Services
Programming Web Services with .Net (Part II)
Programming Web Services with .Net (Part I)

(Registered users can post questions/comments)

 
 TLINKS SEARCH
Advanced Search
Help
 Recommended Links
Red Cross
Responding to hurricane katrina relieve. Donate today. It's a Great Feeling to Help.
http://www.redcross.org
Getusjobs.com
Getusjobs.com is the job site focused on American jobs. See the results that put us on top.
http://www.getusjobs.com
Database Tool
TLinkSoft® tools empowers developers, integrators and DBAs to be more productive.
http://www.cppunit.org/download.jsp
USAnalyst.com
USAnalyst.com provide a community for database analysts, business analysts, developer analysts and managers.
http://www.cppunit.org/article

Powered by Tlinks Systems