Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Develop a web service without an IDE, Part 2: Creating a "Hello World!" web service client on the command line

Alec Go, Software Engineer , EMC
Alec Go is a Software Engineer for IBM WebSphere Application Server Level 2 Support. He has a Bachelor's degree in Computer Engineering from the Pennsylvania State University and graduated with honors.

Summary:  Create a simple "Hello World" standalone Java application web service client using the WSDL2Java command. You'll also learn how to use the TCPMonitor to trace the HTTP messages.

View more content in this series

Date:  24 Jan 2006
Level:  Intermediate
Also available in:   Korean  Japanese

Activity:  12036 views
Comments:  

Introduction

Part 1 of this series focused on creating the web service provider from the command line. In this article, the second in our series, you'll learn how to develop a web service client without an Integrated Development Environment (IDE) like WebSphere® Studio Application Developer (WSAD) or Rational® Application Developer (RAD). You'll see just how simple it is to create a web service client. In the process you will learn how to use the Java™ compiler and WSDL2Java. TCPMonitor is demonstrated to show how to trace HTTP messages.

This article has the following prerequisites:

  1. A Windows-based operating system
  2. WebSphere Application Server 5.x or 6.x. Download the trial version to follow the steps in this article.


Preparation

If you're not continuing from Part 1, install the WAR file linked in the Downloads section, using the WebSphere Application Server Administration Console. If you're continuing from Part 1 and have already installed the WAR file, skip this step. To install the WAR file, follow these steps:

Go to Applications -> Enterprise Applications -> Install. For the Local Path specify the location of HelloWorld.war, and for the Context Root specify HelloWorldWAR.



Figure 1. View of the Admin Console
Admin Console

When the WSDL file was first created, the location parameter was set to "http://localhost:9080/HelloWorldWAR/services/HelloWorld." Therefore, the Context Root must be "HelloWorldWAR" so that the WSDL file can be used to generate clients, without manual alteration.

Click Next through all the defaults. Finish installing the WAR. Remember to save all the changes. Now the most important step is to start the WAR file - this is a step easily forgotten!


Creating the client

Assuming that the web service provider starts up correctly, we can now create a client. There are three basic steps:

  1. Create the client stubs from the WSDL
  2. Develop a Java class that uses the stubs
  3. Compile the Java files and run the client

Create the client stubs from the WSDL

The beauty of the Web Services programming model is that it is very easy to invoke remote methods. The WSDL is used to create a stub, which can then be used to represent the remote object on the client side:

A) Create a directory (c:\temp\Client). Copy HelloWorld.wsdl created from Part 1, or download from the Downloads section.

B) Open a command-line prompt, and navigate to c:\temp\Client. Type in the following commands:


Listing 1. Set up your command-line prompt
			
SET WAS_HOME=C:\Program Files\WebSphere\AppServer5.1

call "%WAS_HOME%\bin\setupcmdline.bat"	

The first line sets the "WAS_HOME" environment variable to where you have WebSphere Application Server installed. Ensure that this points to the correct directory! The second line calls a script that will set up additional environment variables, like JAVA_HOME.

C) Use the WSDL2Java command to create the client stubs:


Listing 2. Running WSDL2Java
call "%WAS_HOME%\bin\WSDL2Java" -role client -verbose -output . HelloWorld.wsdl

Here is an explanation of the options that we use:

  • Role - this can be used to specify either server or client. We choose the latter because we are building a client.
  • Verbose - this prints out messages to the console for every step taken
  • Output - we place a period to specify the current directory
  • WSDL file - this is always the last argument

After running this command, the following will appear in the console:


Listing 3. Output from WSDL2Java
WSWS3185I: Info: Parsing XML file:  HelloWorld.wsdl
WSWS3282I: Info: Generating .\mypackage\HelloWorld.java.
WSWS3282I: Info: Generating .\mypackage\HelloWorldSoapBindingStub.java.
WSWS3282I: Info: Generating .\mypackage\HelloWorldService.java.
WSWS3282I: Info: Generating .\mypackage\HelloWorldServiceLocator.java.
WSWS3282I: Info: Generating .\mypackage\HelloWorldServiceInformation.java.


You will notice that a directory named mypackage will be created with the above files. Here are more details on the files:

  • HelloWorld.java - this is the Service Endpoint Interface
  • HelloWorldSoapBindingStub.java - this is an implementation of the Service Endpoint Interface
  • HelloWorldService.java - this is the Service Interface
  • HelloWorldServiceLocator.java - this is the implementation of the Service Interface

The concept to remember is:

  • Service Endpoint Interface: HelloWorldSoapBindingStub implements HelloWorld
  • Service Interface: HelloWorldServiceLocator implements HelloWorldService

You will learn more about these in the next section, when you build the client.


Develop a Java class that uses the stubs

The next step is to create a class file that can utilize the generated stubs. In the "mypackage" directory, create a file called HelloWorldClient.java. Paste the following code:


Listing 4. HelloWorldClient.java
package mypackage;

public class HelloWorldClient {
    public static void main(String[] args) throws Exception
    	{
	HelloWorldServiceLocator hwlocator = new HelloWorldServiceLocator();
	HelloWorld hw = hwlocator.getHelloWorld();
	String str = hw.sayHello();
	System.out.println(str);
	}
}

Notice that only three lines are used to invoke the web service. Here is an explanation:

A) The HelloWorldServiceLocator is a Service object. In short, the Service object is used to obtain the generated stub. The Service object is created by this line:


Listing 5. Creating HelloWorldServiceLocator
HelloWorldServiceLocator hwlocator = new HelloWorldServiceLocator();

The name of the service object will always be *service name*Locator where *service name* is the name of the service in the WSDL file.

NOTE: In a J2EE context (inside a servlet or EJB), the Service object should be retrieved through JNDI. Since we are using a J2SE client, this is the only way to retrieve the Service object.

B) Next, the client stub needs to be created. The stub represents the remote object. The stub is obtained by the following:


Listing 6. Obtaining the stub
HelloWorld hw = hwlocator.getHelloWorld();

Notice that the method to get the port will always be called get*port name* where *port name* is the name of the port in the WSDL file.

C) Finally, the remote method can be called on the stub. Here, we use the sayHello method on the stub, and put the return value into a String:


Listing 7. Invoking method on stub
String str = hw.sayHello();


Compile and run the client

The next step is to actually compile the client. Issue the following command to compile:


Listing 8. Compiling the classes
"%JAVA_HOME%\bin\javac" -extdirs "%WAS_CLASSPATH%;%WAS_EXT_DIRS%;." mypackage\*.java

Class files should appear in the mypackage directory. Finally, run the Java application:


Listing 9. Running the Java application
"%JAVA_HOME%\bin\java" 
	-Djava.ext.dirs="%WAS_CLASSPATH%;%WAS_EXT_DIRS%;." mypackage.HelloWorldClient

The message "Hello World!" should appear on your console. Congratulations! You invoked the Web Service through your client.


Setting up a TCPMonitor Trace

An extra step that we can take is to examine the data that is going over the wire. WebSphere Application Server comes with a tool called TCPMonitor that allows users to trace HTTP messages. In order to use TCPMonitor, we place it between the client and the server. Currently, the client is interacting directly with the server. We will point the client to interact with TCPMonitor, and TCPMonitor will forward the traffic to the server.

A) In order to take advantage of TCPMonitor, we first have to modify the client. Edit HelloWorldClient.java so that it looks like the following:


Listing 10. HelloWorldClient.java
package mypackage;
import javax.xml.rpc.Stub;
public class HelloWorldClient {
    public static void main(String[] args) throws Exception
    	{
	HelloWorldServiceLocator hwlocator = new HelloWorldServiceLocator();
	HelloWorld hw = hwlocator.getHelloWorld();
	((Stub) hw)._setProperty("javax.xml.rpc.service.endpoint.address",
		"http://localhost:1234/HelloWorldWAR/services/HelloWorld");
	System.out.println(hw.sayHello());
	}
}
		

The line ((Stub) hw)._setProperty("javax.xml.rpc.service.endpoint.address", "http://localhost:1234/HelloWorldWAR/services/HelloWorld"); is added to change the endpoint to which the client will send traffic. The default endpoint address is specified in the HelloWorldServiceLocator.java file, with the following line: private final java.lang.String helloWorld_address = "http://localhost:9080/HelloWorldWAR/services/HelloWorld";
Note that we changed the port from 9080 to 1234. Additionally, we must import the java.xml.rpc.Stub class in order to cast the HelloWorld instance into a Stub object in order to cast the HelloWorld instance into a Stub object.

B) When running TCPMonitor, it is important to use the Java version that came with WebSphere Application Server. Otherwise, you may receive a NullPointerException. Issue the following command:


Listing 11. Running TCPMon
"%JAVA_HOME%" -Djava.ext.dirs="%WAS_EXT_DIRS%" com.ibm.ws.webservices.engine.utils.tcpmon	

C) Specify the following values:


Listing 12. Values in TCPMon
Listen Port - 1234
Target Hostname - localhost
Target Port # - 9080
		

The window should look like this:


Figure 2. Setting values in TCPMonitor
Setting values in TCPMonitor

Next, click the Add button. This will configure TCPMonitor to accept traffic on port 1234 and redirect it to 9080. The listener runs automatically after you click the add button. A tab at the top of the TCPMonitor will appear representing the listener.

D) Recompile and invoke the web service client through the following commands:


Listing 13. Running HelloWorldClient
"%JAVA_HOME%\bin\javac" 
	-extdirs "%WAS_CLASSPATH%;%WAS_EXT_DIRS%;." mypackage\*.java
"%JAVA_HOME%\bin\java" 
	-Djava.ext.dirs="%WAS_CLASSPATH%;%WAS_EXT_DIRS%;." mypackage.HelloWorldClient	

E) Examine the traffic in the TCPMonitor window. The SOAP envelope appears all on one line. The web services engine avoids sending new-line characters in order to save space.


Figure 3. TCPMonitor output
TCPMonitor output

F) In order to make the XML easier to read, you can create a new file called tcptrace.xml and paste the XML into it. Mozilla Firefox or Microsoft Internet Explorer can then be used to open the XML file and view it formatted.


Conclusion

The web service programming model makes it very easy to generate stubs which can then be used by the client application to invoke the web service. There are currently many web services out there for which you can use the technique described here to build a client. Major sites like Google, Amazon, Ebay, and Fedex currently have web services that you can use. Enjoy!


Appendix A: Building the client automatically

The above steps can be automated by using the WebServiceTutorial.bat in the Downloads section. First, modify WebServiceTutorial.bat so that the WAS_HOME directory points to a current WAS installation directory. Next, run the batch file. The output should look similar to the following:


Listing 14. Output from WebServiceTutorial.bat

1. Creating Binding Classes...
WSWS3185I: Info: Parsing XML file:  HelloWorld.wsdl
WSWS3282I: Info: Generating .\mypackage\HelloWorld.java.
WSWS3282I: Info: Generating .\mypackage\HelloWorldSoapBindingStub.java.
WSWS3282I: Info: Generating .\mypackage\HelloWorldService.java.
WSWS3282I: Info: Generating .\mypackage\HelloWorldServiceLocator.java.
WSWS3282I: Info: Generating .\mypackage\HelloWorldServiceInformation.java.
2. Compiling Java Client...
3. Running Java Client...
Hello World!



Downloads

DescriptionNameSizeDownload method
Hello World WAR file to install on server sideHelloWorld.war6KBHTTP
Hello World WSDL fileHelloWorld.wsdl3KBHTTP
Script to build the client automaticallyWebServiceTutorial.bat1KBHTTP
Final codeFinish.zip14KBHTTP

Information about download methods


Resources

Learn

Get products and technologies

Discuss

About the author

Alec Go is a Software Engineer for IBM WebSphere Application Server Level 2 Support. He has a Bachelor's degree in Computer Engineering from the Pennsylvania State University and graduated with honors.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=SOA and web services, WebSphere
ArticleID=102464
ArticleTitle=Develop a web service without an IDE, Part 2: Creating a "Hello World!" web service client on the command line
publish-date=01242006
author1-email=alecgo@us.ibm.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Special offers