SIMPLE JAVA WEB-SERVICE ON TOMCAT5.5 EXAMPLE FOR BEGINNER THAT WORKS!!!

This article takes reader step by step to build her/his own simplest program of Java Web Service.
If all steps are followed properly, then the  program should work.

SETUP YOUR ENVIRONMENT FIRST.

Assuming that you have:

C:\axis-1_4
C:\Program Files\Apache Software Foundation\Tomcat 5.5
C:\jdk5\lib;

Make sure your AXIS has following JAR files:
C:\axis-1_4\webapps\axis\WEB-INF\lib>dir /b
activation.jar
axis-ant.jar
axis.jar
commons-discovery-0.2.jar
commons-logging-1.0.4.jar
jaxrpc.jar
log4j-1.2.8.jar
mail.jar
saaj.jar
wsdl4j-1.5.1.jar

If above highlighted files are missing, then download them separately.
And copy them in C:\axis-1_4\webapps\axis\WEB-INF\lib
NOTE: mail.jar or mailapi.jar should be fine.

set TOMCAT_HOME=C:\Program Files\Apache Software Foundation\Tomcat 5.5

AXIS enable your Tomcat:
Copy “C:\axis-1_4\webapps\axis” folder in %TOMCAT_HOME%\webapps
AXIS itself is an Apache application.
By copying this folder you deploy the AXIS application on Tomcat.
Doing this makes your Tomcat behave like a “Java Web Service” Server.

Verify AXIS Server on Tomcat
Start your Tomcat Server & hit following URL on browser.
http://localhost:8080/axis

You should get Hello! Welcome to Apache-Axis web page.
Click on List to see the list of web services deployed currently.

Set classpath
You need to bring those JAR files in your classpath too.
If not, then will give compilation errors on your command prompt when you try to compile/generate code.
Hence copy all these JAR files to C:\axis-1_4\lib
And set the classpath
C:\>set classpath
classpath=.;c:\jdk5\lib;C:\axis-1_4\lib\axis.jar;C:\axis-1_4\lib\axis-ant.jar;C:
\axis-1_4\lib\commons-discovery-0.2.jar;c:\axis-1_4\lib

YOUR ENVIRONMENT IS NOW SET.
BRACE UP TO WRITE, DEPLOY & EXECUTE THE WEB-SERVICE.

Write your Java Interface
package myapp;

public interface Hello {

public String sayHello(String name);

}

Write your Java Interface Implementation
package myapp;

public class HelloImpl {

public String sayHello(String name) {
return “Hello ” + name;
}

}

So that:
C:\practice\myapp\Hello.java
C:\practice\myapp\HelloImpl.java

Compile interfaces & implementation code before generating WSDL
C:\practice>javac myapp\Hello.java -d .
C:\practice>javac myapp\HelloImpl.java -d .

Generate WSDL
C:\practice>java org.apache.axis.wsdl.Java2WSDL -o hello.wsdl -l”http://localhost:8080/axis/services/hello” -n urn:hello -p”hello” urn:hello myapp.Hello
Notice that hello.wsdl got generated.

Generate glue code
C:\practice>java org.apache.axis.wsdl.WSDL2Java -o . -d Session -s -p myapp.ws hello.wsdl
So that:
C:\practice\myapp\ws>dir /b
deploy.wsdd
Hello.java
HelloService.java
HelloServiceLocator.java
HelloSoapBindingImpl.java
HelloSoapBindingStub.java
undeploy.wsdd

Time to modify the glue code a bit
Notice “HelloSoapBindingImpl.java“.
It is the implementation that is automatically generated.
But you already have written your own implementation which is: HelloImpl.java
So, call your Impl method in generated Impl method,
Don’t forget to copy your HelloImpl.java in location C:\practice\myapp\ws\
And change its package form myapp to myapp.ws because now it’s been moved there.

/**
* HelloSoapBindingImpl.java
*
* This file was auto-generated from WSDL
* by the Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter.
*/

package myapp.ws;

public class HelloSoapBindingImpl implements myapp.ws.Hello{
public java.lang.String sayHello(java.lang.String in0) throws java.rmi.RemoteException {
System.out.println(“remote service invoked!”);
HelloImpl h = new HelloImpl();
return h.sayHello(in0);
}

}

Compile the glue code
C:\practice>javac myapp\ws\*.java
May show some warnings but that should be fine.

Package into JAR
C:\practice>jar cvf hello.jar myapp/*.class myapp/ws/*.class
Move hello.jar
C:\practice>move hello.jar “c:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\axis\WEB-INF\lib
Start Tomcat server before deploying

Deploy the service
C:\practice>java org.apache.axis.client.AdminClient myapp\ws\deploy.wsdd

Verify deployed service
Re-Start your Tomcat Server & hit following URL on browser.
http://localhost:8080/axis

You should get Hello! Welcome to Apache-Axis web page.
Click on List to see the list of web services deployed currently containing your hello service.

Write the Client
package myapp.client;

public class HelloServiceClient {
public static void main(String[] args){
try {
//Get the service.
myapp.ws.HelloService service = new myapp.ws.HelloServiceLocator();

// Get the stub.
myapp.ws.Hello hello = service.gethello();

// Make the call.
System.out.println(hello.sayHello(“some name”));
}
catch(Exception e) {
e.printStackTrace();
}
}
}

Compile the client
javac myapp\*.java -d .

Execute the client
C:\practice>java myapp.client.HelloServiceClient

Output should be:
Hello some name

Recommended references:
http://www.onjava.com/lpt/a/1578
http://www.ociweb.com/mark/JavaUserGroup/Axis.pdf
http://jaitechwriteups.blogspot.com/2006/12/simple-webservice-on-jboss-using-axis.html

Anup Jani
(Bsc, MCP, GNIIT, SCJP, SCWCD, IBM OOAD/UML JCert.)
My profile: http://www.linkedin.com/in/anupjani
My Blog: http://www.anupjani.wordpress.com

Advertisements

12 Comments

  1. Posted June 8, 2009 at 11:08 pm | Permalink | Reply

    This is great, thanks!
    Do you see any issues with using Tomcat 6 and JDK 6?
    Thanks,
    Steve

    • anupjani
      Posted June 9, 2009 at 11:31 pm | Permalink | Reply

      Thanks for comments Steve. I don’t remember when I have tried that combination. Don’t forget to consider AXIS version too when you try yourself. Version conflicts is often a big problem in heterogeneous open source applications.

  2. Posted June 9, 2009 at 3:56 pm | Permalink | Reply

    On attempting to generate the WSDL I received the following message:
    “Could not find the main class: org.apache.axis.wsdl.Java2WSDL.”

    Does the computer have to be attached to the Internet at the time of generating the WSDL? this computer was not. Thanks for your assistance,
    Steve

    • anupjani
      Posted June 9, 2009 at 11:36 pm | Permalink | Reply

      Connect to Internet? Of course not. A class is not found obviously means either or all of this:
      1. AXIS library doesn’t fall in your classpath
      2. Some JAR files are still missing.
      Double check your “Environment Setup” as mentioned in this article.

  3. MuraliMohan
    Posted October 23, 2009 at 2:38 pm | Permalink | Reply

    Very Nice POC for Java Webservice Devs… those who want to know the using of AXIS in there applicaion.

    Thanks for Sharing Us This POC.

    Thanks,
    Murali.

  4. dinesh sood
    Posted March 3, 2010 at 12:00 pm | Permalink | Reply

    Its a wonderful example.
    I tried it for the first time and
    it worked without ny problem.
    Although i ran this example finely. But i cld’t undersatant, some of the commands i.e what is there purpose.
    It would be nice if u please give some explanation regarding some commands like

    “java org.apache.axis.wsdl.WSDL2Java -o . -d Session -s -p myapp.ws hello.wsdl
    ” etc.

    Thanks,
    Dinesh Sood

  5. Kishore
    Posted August 26, 2010 at 12:33 pm | Permalink | Reply

    Good article for starter’s.

  6. Smitty
    Posted October 9, 2010 at 11:26 am | Permalink | Reply

    On running the client i get the….
    Sockect exception: Invalid argunent “connect” …..
    Wat could be the reason for this……..

  7. Suman M K
    Posted June 16, 2011 at 3:59 am | Permalink | Reply

    Good most. Can we get the level of web Services 🙂

    Thanks,
    Suman

  8. Posted July 12, 2011 at 1:51 pm | Permalink | Reply

    thanks for ur poc

  9. Sowjanya
    Posted April 10, 2012 at 2:12 pm | Permalink | Reply

    very good poc . Its helps me alot

  10. George
    Posted April 13, 2012 at 4:46 pm | Permalink | Reply

    U r the man!….thanks a lot….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: