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

Programming Java Web Services (II)
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. We have learned to generate WSDL (Web Services Description Language) file from the web service we developed.

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. We will learn how to access the web service, from both the desktop client applications and the web applications.

This article assumes readers have completed Axis and Tomcat installation. If you did not 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.

1. Access web services from desktop client applications

There are two ways to generate client codes connecting to web services. One is dynamically calling web service within client applications. The other is using service stub in the client applications.

1.1 Dynamically invoking web services

We use the web service of last article "Programming Java Web Service", which has URL as http://localhost:8080/axis/HelloWorld.jws

The client code is listed as

List 1. TestClient.java


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

public class TestClient {
public static void main(String[] args) {
try {
String location="http://localhost:8080/axis/HelloWorld.jws";
Service service=new Service();
Call call=(Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(location));
call.setOperationName("sayHelloWorld");
String myString= (String) call.invoke(new Object[] {"myString"} );
System.out.println(myString);
} catch (Exception e) {
System.err.println(e.toString());
}
}

}

Explanation

Before we explain the client codes, we want make sure all required libraries are included in development environment. Many errors could be output if you did not do these steps carefully.

In my system, I use axis 1.2. When you compile the client code above, make sure that the files in axis libraries (C:\axis-1_2/lib on my system since I installed axis 1.2 at C:\axis-1_2) are in classpath of your machine. Below are the jar files within my axis lib folder.


Please note that I have included xerces jar files and mailapi.jar in my axis lib folder C:\axis-1_2/lib

To compile above file, you may add all jar files in axis lib to your classpath variable, then use

javac TestClient.java

I have also written an ant build file compile.xml for building this class.

List 2. compile.xml


<project name="HelloWorld" 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>

By running

ant -buildfile compile.xml

On a windows console, you can build this class file successfully, assuming both compile.xml and TestClient.java is in foleder D:\myAxis-Exercise\HelloWorld1 folder (You may put them both in another folder, such as C:\myAxisExercises\HelloWorld1).


Now start apache tomcat 5 as we did in the article "Programming Java Web Service".

Type command

java TestClient

You should be able to see output:

Hello World

1.2 Use a service stub

Now we tried to use a service stub in our client application.

We will follow following steps to write the client application

1. You need find the service end point.

2. Use WSDL2Java to generate stub classes.

3. Write a client application to use web service.

4. Compile classes


1.2.1 Service End Point

The service we are using is

http://localhost:8080/axis/HelloWorld.jws

We can use

http://localhost:8080/axis/HelloWorld.jws?WSDL

to get service WSDL file.

1.2.2 Using WSDL2Java to generate stub classes

Create a folder C:\Axis-Exercises\Hello2, and open a command console in Windows.

The full command for our example becomes:


java org.apache.axis.wsdl.WSDL2Java -o . -d Session http://localhost:8080/axis/HelloWorld.jws?WSDL

If you have set up path and classpath correctly following step-by-step instructions in my
previous article "Programming Java Web Service", you should see four classes have generated in the folder C:\Axis-Exercises\Hello2\localhost\axis\HelloWorld_jws


HelloWorld.java
HelloWorldService.java
HelloWorldServiceLocator.java
HelloWorldSoapBindingStub.java


Explanation

WSDL2Java class has several options. "-o ." directs the output files under current directory. In

our example, the current directory is folder C:\Axis-Exercises\Hello2.

"-d Session" tells the scope of application is "Session". It is optional here http://localhost:8080/axis/HelloWorld.jws?WSDL is the wsdl file based on which the stub files are generated. You may also use a file name for this parameter if you have saved a wsdl file locally.

More options, such as "-p" can be used to generate a package name you assigned, instead of the default package name. Next article I will use more options to generate web services.

1.2.3 Creating the client application

Also we create a client application SayHello.java based on service stub classes. SayHello.java uses stub classes that have been generated. We put SayHello.java in folder C:\Axis-Exercises\Hello2

List 3. SayHello.java


import localhost.axis.HelloWorld_jws.*;

class SayHello {

public static void main(String args[]) {
try {
HelloWorldServiceLocator hwsl=new HelloWorldServiceLocator();
HelloWorld hw=hwsl.getHelloWorld();
System.out.println(hw.sayHelloWorld("My Hello World"));
} catch (Exception e) {
System.out.println("Exception caught!");
}
return;
}
}


Explanation

Class HelloWorldServiceLocator was used to obtain an object of HelloWorld, which in turn calls the method sayHelloWorld to print out "Hello World".

1.2.4 Compiling classes

I use the ant build file compile.xml as before. Copy this file into folder C:\Axis-Exercises\Hello2

Open a command console, and type


Ant -buildfile compile.xml

All class files are generated with the directory C:\Axis-Exercises\Hello2

Now type


Java SayHello

In the command line, you can see "Hello World" has been the output.


2. Access web services from web browsers

Web services can easily be used in web applications where they will be accessed by web browsers.

HelloWorldServiceLocator.java has the service endpoint URL.

We copy the whole stub classes into our web application server classes path.

In my computer, I have a web application called myapp. You may create a web application by yourself. Or you can get my code from article Introduction To Struts By Example on
http://www.usanalyst.com. I copy the folder localhost from C:\Axis-Exercises\Hello2 to myapp\WEB-INF\classes (You may also make classes in folder localhost as a jar file and copy the jar file to myapp\WEB-INF\lib directory).

Because our classes will use classes in axis library, I copy the jar files in C:\axis-1_2\lib to myapp\WEB-INF\lib directory.

I wrote a jsp application Hello.jsp to show web service output.

List 4. Hello.jsp


<%@ page import="localhost.axis.HelloWorld_jws.*" %>
<%
try {
HelloWorldService hwsl=new HelloWorldServiceLocator();
HelloWorld hw=hwsl.getHelloWorld();
out.println("<b>"+hw.sayHelloWorld("My Hello World")+"</b>");
} catch (Exception e) {
out.println("Exception caught!");
}
%>

Now, restart Tomcat web application server, and type

http://localhost:8080/myapp/Hello.jsp

in the web browser.

We can see "Hello World" displayed in the browser.

3. Access online web service

To make readers easier to access web service, I have created a public available endpoint for our web service, which is
http://www.cppunit.org:8081/axis/HelloWorld.jws

I also write a JSP page that utilized this web service http://www.cppunit.org/article/Hello.jsp

You may click this link to see "Hello World" that was retrieved from a web server can be easily incorporated into a web page.

4. Conclusion

In this article I have demonstrated 2 ways to access web services, dynamically invoking web services and invoking web services with service stub classes.

After this article, readers should be able to write client applications accessing different web services.


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