http://in.geocities.com/rsramsam/xmlrpc.htm

 

     Understanding XML RPC

             (published in DeveloperIQ…  March-2004)

                 (www.developeriq.com)

 

R.S.RAMASWAMY

(rsramsam@yahoo.co.in)

 

         There is much importance given now to XML-webservice because the industry has now realized that traditional solutions like COM+, CORBA and EJB, while still relevant, are not enough. This is because they do not provide inter-operability with other systems. XML is not proprietary and XML parsers are available for all platforms and languages. So, it is now felt that it will be better to expose EJB or CORBA (or ASP.NET with COM+) as an XML-webservice.

        The basic structure is retained and for the purpose of interacting with other systems, the same is exposed as an XML-webservice too. It should not be understood that the traditional schemes have become obsolete and have been replaced by XML-webservice. Not at all! Both are necessary.

 

        As this topic has become very important now, we will attempt to acquaint ourselves with the evolution of this idea from XML-RPC through SOAP and finally as AXIS. It is apt that we are dealing with these three in this issue, as all of them are from Apache Free/Open Software foundation.

 

         Let us begin with XML-RPC. RPC stands for Remote Procedure Call. “Sun Microsystems is usually given the credit for creating a generic formal mechanism used to call procedures and return results over a network”. We have already seen a number of systems like RMI, CORBA, and RMI-IIOP, which are all advanced variants of RPC. In all these schemes, the parameters are passed to the remote-server in binary form and the remote-server sends the result back to the client in binary form. While this ensures a good level of security, it also makes inter-operability difficult. In XML-RPC, the parameters are simple primitives and sent as XML. Similarly, the remote sever sends the result as primitive and in XML form. SOAP (Simple Object Access Protocol) is an extension of XML-RPC and we can send objects as parameters and get the result as objects, in XML form. In both the schemes the function call also is in XML form.

 

        XML-RPC can be used to connect a Java program with a Perl/Python/ASP program! This is very lightweight and simple. For example, a java program running in windows can make use of a function written in Perl and available in a Unix machine, without using CORBA and using only XML and http port 80. [Readers may refer to a very well written book from O’Reilly press (“Programming WebServices with XML-RPC” by Simon St.Laurent) and also a fine chapter from “Java & XML” by McLaughlin form the same publishers.]

 

   In our experiment, we will e using Apache XML-RPC.

To quote:

 

        “Apache XML-RPC is a java implementation of XML-RPC <http://www.xmlrpc.com>, a popular protocol that uses XML over HTTP to implement remote procedure calls.

 

         Apache XML-RPC was previously known as Helma XML-RPC .If you have code using the helma library, all you have to do is change the import statements in your code from helma.xmlrpc.*  to org.apache.xmlrpc.*.

 

 

          Apache XML-RPC supports SAX 1.0. The default parser is John Wilson’s MinML parser, which is included in the package; so you don’t need anything else to start using the software. MinML is an ideal parser of XML-RPC because it is small, fast and implements exactly the features of XML, which are used by XML-RPC.”

 

 

           We download the XML-RPC from the Apache website listed above. It is a zip file of size just above 300 KB and we can unzip and install the files in e:\xmlrpcpack folder. When unzipped it was about 1.5MB.

 

            This folder contains XML-RPC-1.1.jar, and we need to include this in our classpath.We can rename it as xmlrpc.jar

 

Let us create a folder e:\xmlrpc\demo1

cd to e:\xmlrpc\demo1. In that window,

set path and classpath as given below.

 

    >set path=c:\windows\command;d:\jdk1.3\bin;

         (assuming that jdk1.3 has been installed in d:\)

     >set classpath=e:\xmlrpc\demo1;

                    e:\xmlrpcpack\xmlrpc.jar;

 

Create and compile the following files:

 

                hellohandler.java (Code 1)

                helloserver.java (Code 2)

                helloclient.java (Code 3)

 

Code 1

 

public class hellohandler

{

  public String greetme(String s)

  {

         return “how are you?…”+s;

  }

}

 

 

 

 

Code 2

 

     //   helloserver.java

 

import java.io.*;

import org.apache.xmlrpc.*;

 

public class helloserver

{

   public static void main(String args[])

 {

   try

   { 

       System.out.println(“server being started”);

     

       WebServer    server = new WebServer(1234);

     

       System.out.println(“server started …ok”);

       System.out.println(“listening in port 1234”);

       server.addHandler(“greeter”,new hellohandler());

       System.out.println(“handler added…ok”);

 

       System.out.println(“waiting for client”);

      }catch(Exception e1){

    System.out.println(“”+e1);}

  }

}

 

Is this not ultra-simple?

 

Let us now create the client file. (Code 3)

*******************************************************

Code 3

 

//  helloclient.java

 

import java.io.*;

import java.util.*;

import org.apache.xmlrpc.*;

public class helloclient

{

  public static void main(String args[])

{

   try

    {

        XmlRpcClient   client  =

            New XmlRpcClient(“http://localhost:1234/”);

         Vector      vector1=new Vector();

              vector1.addElement(args[0]);

 

         Object  ob=

                client.execute(“greeter.greetme”,vector1);

    

         System.out.println(“”+ob);

     }catch(Exception e1) 

       {System.out.println(“”=e1);}

  }

}

*********************************************************

After creating these files, we can compile them in just one step

  demo1>javac *.java

 

We are nw ready to run the program.

Start the remote server…demo1>java helloserver

Open another dos window and set path and classpath as before.

 

Execut the client program and pass parameter ‘sam’ as argument. >java    helloclient   sam

 

 You’ll get the result as “how are you?…sam”.

 

Thus we have tested our simple program. If we compare the procedure and code with what we have already seen in the context of socket example, RMI etc., we cannot but be struck by the similarity of concept. But, here the data is sent and obtained as XML. (Thankfully, however, the XML part is completely hidden from us by the creators of the library!). Thus, we need not know much about XML to program using XML-RPC.

      It will be more useful if the client is written as a servlet, as in that case, the program can e invoked by anyone through a browser. So we create a client as a servlet as shown below  (Code 4).

Code 4

 

//  helloclientservlet.java

(in e:\xmlrpc\demo1 folder)

 

import java.servlet.*;

import javax.servlet.*;

 

import java.sql.*;

import java.util.*;

import java.io.*;

import org.apache.xmlrpc.*;

public class helloclientservlet extends HttpServlet

{

   public void doPost(HttpServletRequest request,

                                   HttpServletResponse response)

                            throws ServletException,IOException

  {

       response.setContentType(“text/html”);

       PrintWriter   out=response.getWriter();

//======================================

 

String s=request.getParameter(“text1”);

try

{

        XmlRpcClient   client  =

            New XmlRpcClient(“http://localhost:1234/”);

         Vector vector1=new Vector();

              vector1.addElement(args[0]);

 

         Object  ob=

                        client.execute(“greeter.greetme”,vector1);

         out.println(“”+ob);

   }

   catch(Exception e1){out.println(“”=e1);}

 }

}

 

 

 We will not be able to comile this file unless we change the classpath.

 

  demo1>set classpath=%classpath%;d:\jsdk2.0\src;

 

(It is assumed that we have installed jsdk2.0in d:\)

 

Now we will be able to compile the servlet. We now create the html file to invoke the servlet. (Code 5)

Code 5

// helloclientservlet.htm

<html>

<body>

<form method=post   action=”http://localhost:8080/servlet/helloclientservlet”>

<input type=text       name=”text1”>

<input type=submit>

</form>

</body>

</html>

 

We have to start the webserver now.

Open another dos window.

Change directory to:

D:\jsdk2.0\bin

Remember to set classpath in this window as:

  bin>set classpath=e:\xmlrpcpack\xmlrpc.jar;

Verify the classpath: in>echo %classpath%

 

We will get e:\xmlrpcpack\xmlrpc-.1.jar

Now, start the webserver:

..bin>servletrunner –d  e:\xmlrpc\demo1 –r  e:\xmlrpc\demo1

(-d refers to folder in which servlet’s class file is available)

(-r refers to folder in which html file is available)

This starts the webserver in port 8080.

We can start the browser now and type the Url as:

  ‘e:/xmlrpc/demo1/helloclientservlet.htm’

We get a form.We type our name in text1.we get the greeting message.

  So, what is so special? The point is that,either the server or the client may be java/perl/python/asp etc..!

  A number of practical examples with codes have been given in Simon’s book.Readers will find  them very interesting and integrating Windows and Linux!

 

                          HOME PAGE

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1