Java Tutorial - Java Script : Creating an XML-RPC Web Service:

Java Tutorial - Java Script :
An XML-RPC server is a program that receives a request from a client, calls a method in response to that request, and returns the result. The server maintains a list of methods that it allows clients to call on; these are different Java classes called handlers. Apache XML-RPC handles all the XML and networking itself, enabling you to focus on the task you want a remote method to accomplish. There are several ways to serve methods remotely with the classes in the org.apache. xmlrpc package. The simplest is to use the WebServer class, which represents a simple HTTP web server that only responds to XML-RPC requests. This class has two constructors:

·         WebServer(int)—Create a web server listening on the specified port number.

·         WebServer(int, InetAddress)—Create a web server at the specified port and IP address. The second argument is an object of the java.net.InetAddress class.

Both constructors throw IOException exceptions if there’s an input/output problem creating and starting the server. The following statements create an XML-RPC web server on port 4413:


The web server does not contain the remote methods that clients call via XML-RPC. These reside in other Java classes, which are called handlers. To add a handler, call the server’s addHandler(String, Object) method with the specified handler name and handler object. The first argument to addHandler() is a name to give the handler, which can be anything— it’s comparable to naming a variable. Clients will use this name when calling remote methods. The SiteClient application created earlier today called the remote method dmoz.getRandomSite(). The first part of this call—the text preceding the period—refers to a handler named dmoz.

The second argument to addHandler() is an object of the class that has public methods, which can be called remotely. The following statements create a handler for a WebServer object named server:


The handler in this example is a DmozHandler object, which contains a getRandomSite() method that returns information about a random site in the Open Directory Project. You’ll be creating this class later. A class that handles remote method calls can be any Java class that contains public methods that return a value, as long as the methods take arguments that correspond with data types supported by Apache XML-RPC:

boolean, byte[], Date, double, Hashtable, int, String, and Vector.

 You can easily put existing Java classes to use as XML-RPC handlers without modification as long as they do not contain public methods that should not be called and each public method returns a suitable value.

Using Apache XML-RPC, the web server allows any public method in the handler to be called, so you should use access control to keep prying clients out of methods that should remain off limits.

As the first step toward the creation of an XML-RPC service, the following code creates a simple web server that takes XML-RPC requests. Enter the text of Listing 20.4 and save the file as DmozServer.java.

This class can’t be compiled successfully until you have created the handler class DmozHandler. The DmozServer application takes a port number as a command-line argument and calls the startServer() method with this argument.

The startServer() method creates a WebServer object that monitors that port number for incoming XML-RPC requests. One handler is added to the server:  a DmozHandlerobject given the name “dmoz”; then the server’s start() method is called to begin listening for requests. That’s all the code required to implement a functional XML-RPC server.

 Most of the work is in the remote methods you want a client to call, which don’t require any special
techniques as long as they are public and they return a suitable value. To give you a complete example you can test and modify to suit your own needs, the DmozHandler class is provided.


The techniques employed in this class were covered during Day 18, “Accessing Databases with JDBC,” and are a good review of how to use JDBC to retrieve records from a database—in this example a MySQL database called db1. Enter the text of Listing 20.5 and save the file as DmozHandler.java; then compile the classes DmozServer.java and DmozHandler.java.
Lines 28–32 of the DmozHandler application should be changed to reflect your own database drive, username, and password. You also might need to change the rest of the string used to connect to the database, depending on your driver. The server is run by specifying the port number at a command line, as in this example:

java DmozServer 4413

 After the server is up and running, you can modify the SiteClient application to connect to a different XML-RPC server. Change lines 36–37 of Listing 20.3 to replace localhost:4413 with a reference to the server’s computer followed by a colon and port number, as in the following:

XmlRpcClient client = new XmlRpcClient( “http://cadenhead.org:4413/”);