Writing your first program in Java SOAP
Writing your first program in Java SOAP
---------------------------------------
1) Setup tomcat on your machine. This will be your app container.
2) Write HelloWorld.java (see below). This then becomes the server-side program that will serve the client requests. Compile this and put the helloworld.jar file in your
tomcat's lib directory.
3) Write HelloClient.java (see below). This is the remote client that needs to access the services of HelloWorld.java.
Now we need a router to route client request to the server-side jar helloworld.jar.
4) Download soap.war (from apache) and put it in your tomcat webapps directory. This is the router for soap requests.
PS: To confirm if it is actually installed, point your browser to /soap/admin/index.html. You should see a page which lists all deployed SOAP apps.
5) Next step is to register the HelloWorld server program with the router.
java org.apache.soap.server.ServiceManagerClient http://localhost:8180/soap/servlet/rpcrouter deploy HelloWorld.xml
(see HelloWorld.xml below).
PS: Check if the server is registered or not here:
http://localhost:8180/soap/admin/index.html
6) Run the client:
java HelloClient.java http://localhost:8180/soap/servlet/rpcrouter MyName
Enjoy!
//////////////////// HelloClient.java /////////////////////////
public class HelloClient {
public static void main (String[] args)
throws Exception {
System.out.println("\n\nCalling the SOAP Server to say hello\n\n");
System.out.println("\n\nCalling the SOAP Server to say hello\n\n");
URL url = new URL (args[0]);
String name = args[1];
Call call = new Call ( );
call.setTargetObjectURI("urn:HelloWorld");
call.setMethodName("sayHello");
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
Vector params = new Vector ( );
params.addElement (new Parameter("name", String.class, name, null));
call.setParams (params);
System.out.print("The SOAP Server says: ");
Response resp = call.invoke(url, "");
if (resp.generatedFault ( )) {
Fault fault = resp.getFault ( );
System.out.println ("\nOuch, the call failed: ");
System.out.println (" Fault Code = " + fault.getFaultCode ( ));
System.out.println (" Fault String = " + fault.getFaultString ( ));
} else {
Parameter result = resp.getReturnValue ( );
System.out.print(result.getValue ( ));
System.out.println( );
}
}
}
//////////////////// HelloClient.java /////////////////////////
//////////////////// HelloWorld.java /////////////////////////
public class HelloWorld {
public String sayHello(String name) {
return "Hola " + name;
}
}
//////////////////// HelloWorld.java /////////////////////////
//////////////////// HelloWorld.xml /////////////////////////
id="urn:HelloWorld">
scope="Application"
methods="sayHello">
org.apache.soap.server.DOMFaultListener
//////////////////// HelloWorld.xml /////////////////////////
Useful links:
http://www.xml.com/pub/a/2002/01/30/soap.html
http://www.oreilly.com/catalog/progwebsoap/chapter/ch03.html
http://ws.apache.org/soap/
---------------------------------------
1) Setup tomcat on your machine. This will be your app container.
2) Write HelloWorld.java (see below). This then becomes the server-side program that will serve the client requests. Compile this and put the helloworld.jar file in your
tomcat's lib directory.
3) Write HelloClient.java (see below). This is the remote client that needs to access the services of HelloWorld.java.
Now we need a router to route client request to the server-side jar helloworld.jar.
4) Download soap.war (from apache) and put it in your tomcat webapps directory. This is the router for soap requests.
PS: To confirm if it is actually installed, point your browser to /soap/admin/index.html. You should see a page which lists all deployed SOAP apps.
5) Next step is to register the HelloWorld server program with the router.
java org.apache.soap.server.ServiceManagerClient http://localhost:8180/soap/servlet/rpcrouter deploy HelloWorld.xml
(see HelloWorld.xml below).
PS: Check if the server is registered or not here:
http://localhost:8180/soap/admin/index.html
6) Run the client:
java HelloClient.java http://localhost:8180/soap/servlet/rpcrouter MyName
Enjoy!
//////////////////// HelloClient.java /////////////////////////
public class HelloClient {
public static void main (String[] args)
throws Exception {
System.out.println("\n\nCalling the SOAP Server to say hello\n\n");
System.out.println("\n\nCalling the SOAP Server to say hello\n\n");
URL url = new URL (args[0]);
String name = args[1];
Call call = new Call ( );
call.setTargetObjectURI("urn:HelloWorld");
call.setMethodName("sayHello");
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
Vector params = new Vector ( );
params.addElement (new Parameter("name", String.class, name, null));
call.setParams (params);
System.out.print("The SOAP Server says: ");
Response resp = call.invoke(url, "");
if (resp.generatedFault ( )) {
Fault fault = resp.getFault ( );
System.out.println ("\nOuch, the call failed: ");
System.out.println (" Fault Code = " + fault.getFaultCode ( ));
System.out.println (" Fault String = " + fault.getFaultString ( ));
} else {
Parameter result = resp.getReturnValue ( );
System.out.print(result.getValue ( ));
System.out.println( );
}
}
}
//////////////////// HelloClient.java /////////////////////////
//////////////////// HelloWorld.java /////////////////////////
public class HelloWorld {
public String sayHello(String name) {
return "Hola " + name;
}
}
//////////////////// HelloWorld.java /////////////////////////
//////////////////// HelloWorld.xml /////////////////////////
methods="sayHello">
//////////////////// HelloWorld.xml /////////////////////////
Useful links:
http://www.xml.com/pub/a/2002/01/30/soap.html
http://www.oreilly.com/catalog/progwebsoap/chapter/ch03.html
http://ws.apache.org/soap/
