Java XML-RPC: Using XmlRpcServlet

Apache XML-RPC also provides xmiRpcServiet class, which has an embedded instance of xmiRpcServer. This servlet may be installed to an existing web server (such as tomcat) to serve XML-RPC requests. Note that Apache’s tomcat is an extremely versatile web server and full blown servlet engine. Its performance is not questionable even under high load. So, it is a good choice to use this built-in servlet instead of using light-weight Webserver class.

In this section, we shall discuss how to make use of this servlet to serve XML-RPC requests. We assume that tomcat is already installed in your computer. The detailed installation procedure may be found in Chapter 20. Consider the home directory of apache tomcat is D:\apache-tomcat- 8.0.0-RCi. Hereafter, we shall refer to this directory as tomcat_home. Create the following directory structure under webapps sub directory of tomcat_home. Place all XML-RPC .jar files (i.e. commons- logging-1.1.jar, ws-commons-util-1.0.2.jar, xmlrpc-client-3.1.3.jar, xmlrpc-common- 3.1.3.jar, xmlrpc-server-3.1.3.jar) in WEB-INF\lib directory. Alternatively, these .jar files may be placed in the lib subdirectory of tomcat_home directory. Inside the classes directory, place the file FactImpl.class.

Now, create a property file XmlRpcServlet.properties under webserver directory.

Note that the xmlRpcServiet always reads the object mapping information from a file named xmiRpcServiet.properties, which must be stored in the directory org/apache/xmirpc/ server/webserver/, relative to a directory recognized by tomcat.

Alternatively, this property file may be added to any of the .jar files in lib directory. Enter the following line in this file.

Factorial=FactImpl

The XmlRpcServlet reads this property file and binds objects with names as specified in the file. The left-hand side of ‘=’ sign is the name of the object whose class name is written to the right side of ‘=’. In our case, the name of object is Factorial and its value is the fully qualified name of the FactImpl class. This name is used by the client during method invocation. Now, append the following entries to <web-app>… </web-app> element in the war file wEB-iNF\web.xml file.

<servlet>

<servlet-name>XmlRpcServlet</servlet-name> <servlet-class>

org.apache.xmlrpc.webserver.XmlRpcServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>XmlRpcServlet</servlet-name>

<url-pattern>/servlet</url-pattern>

</servlet-mapping>

You may take a look at the sample web.xml file stored in tomcat_home\webapps\ROOT\wEB-iNF directory. Start tomcat using executing startup.bat in the tomcat_home\bin directory. If everything goes fine, our XmlRpcServlet is now ready to serve XML-RPC requests. The URL of this servlet is:

http://”+args[0]+”:8080/xmlrpc/servlet

We assume that the IP address of the computer where this tomcat web server runs is given as a command line argument. We also assume that tomcat listens on port 8080 for HTTP requests. Use the above URL to modify either Clienti.java or Client2.java and store it in another file Client3. java. The complete source code is given below:

//Client3.java

import org.apache.xmlrpc.client.*;

import java.net.URL;

public class Client3 {

public static void main (String [] args) {

try {

XmlRpcClient client = new XmlRpcClient();

XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();

config.setServerURL(new URL(“http://”+args[0]+”:8080/xmlrpc/servlet”));

client.setConfig(config);

int n = 6;

Object[] params = new Object[]{ new Integer(n)};

System.out.println(“Sent : “+n);

Integer result = (Integer)client.execute(“Factorial.fact”, params);

System.out.println(“Received : “+result);

} catch (Exception e)

{
e.printStackTrace();

}

}

}

We can now compile and start this program. A sample result of tomcat terminal and Client3 terminal is shown in Figure 17.8:

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *