Apache XML-RPC 3.1.3 step by step
December 11th, 2010 . by adminHere is my first test with XML-RPC 3.1.3
- Server is Java XmlRpcServlet.
- Client is a simple Java client
The example come from Apache http://ws.apache.org/xmlrpc/server.html
1. Create a new Project in Eclipse
for example in Eclipse Helios click File->New->Dynamic Web Project
Project name: xmlrpctest
Target runtime is Apache Tomcat v6.0
Click Finish
2. Download the software
From page http://ws.apache.org/xmlrpc/download.html click distribution directory and choice a mirror which apache suggest
exm http://apache.myamplifiers.com//ws/xmlrpc/
Choice apache-xmlrpc-current-bin.zip. The current version is XML-RPC 3.1.3
3. Copy lib files
Unpack the file apache-xmlrpc-current-bin.zip
copy all following files to the lib directory of project xmlrpctest,
which you can find it in workspace directory of Eclipse
Directory of C:\Java\ws\xmlrpctest\WebContent\WEB-INF\lib 12/10/2010 09:47 AM <DIR> . 12/10/2010 09:47 AM <DIR> .. 09/11/2009 02:43 PM 52,915 commons-logging-1.1.jar 09/11/2009 02:43 PM 34,407 ws-commons-util-1.0.2.jar 02/06/2010 05:13 PM 58,573 xmlrpc-client-3.1.3.jar 02/06/2010 05:12 PM 109,131 xmlrpc-common-3.1.3.jar 02/06/2010 05:14 PM 81,555 xmlrpc-server-3.1.3.jar
in Eclipse right click the project xmlrpctest->Refresh or press F5.
you will see the 5 jar files in xmlrpctest.WebContent.Web-INF.lib
and in xmlrpctest.Java Resources src.Web App Libraries
| Eclipse Project Explorer |
|---|
![]() |
4. Create class Calculator
Create package: org.apache.xmlrpc.demo
-Righ click project xmlrpctest->New->Package
-Dialog New Java Package->name: org.apache.xmlrpc.demo
-Finish
Create class Calculator
-Right click package org.apache.xmlrpc.demo->New->Class
-Dialog New Java Class->name: org.apache.xmlrpc.demo
-Finish
Source of Calculator.java:
package org.apache.xmlrpc.demo;
public class Calculator {
public int add(int i1, int i2) {
return i1 + i2;
}
public int subtract(int i1, int i2) {
return i1 - i2;
}
}
5. Create properties file XmlRpcServlet.properties
The property file must be called XmlRpcServlet.properties, and it must be located in the package org.apache.xmlrpc.webserver
Create package: org.apache.xmlrpc.webserver
-Righ click project xmlrpctest->New->Package
-Dialog New Java Package
package name: org.apache.xmlrpc.webserver
-Finish
Create file XmlRpcServlet.properties
-Right click src->New->Other..->File->Next
-File name: XmlRpcServlet.properties
-Finish
File XmlRpcServlet.properties:
Calculator=org.apache.xmlrpc.demo.Calculator
6. Edit file web.xml
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>xmlrpctest_run</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>XmlRpcServlet</servlet-name> <servlet-class>org.apache.xmlrpc.webserver.XmlRpcServlet</servlet-class> <init-param> <param-name>enabledForExtensions</param-name> <param-value>true</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>XmlRpcServlet</servlet-name> <url-pattern>/xmlrpc</url-pattern> </servlet-mapping> </web-app>
7. Create a client program
Right click package org.apache.xml.rpc.demo->New Class
Class name: SimpleClient
SimpleClient.java
package org.apache.xmlrpc.demo;
import java.net.URL;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
public class SimpleClient {
public SimpleClient() {
try {
System.out.println("Try to add 2 + 3 via XML-RPC...");
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL(
"http://127.0.0.1:8080/xmlrpctest/xmlrpc"));
XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);
Object[] params = new Object[] { new Integer(2), new Integer(3) };
Integer result = (Integer) client.execute("Calculator.add", params);
System.out.println("The returned values is: " + result);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new SimpleClient();
}
}
8. Run Test
-Open server view Window->Show Views->Servers
-Add project xmltest to Tomcat Sever
.Right click Tomcat v6.0 at localhost"->Add and Remove..
.Choice/Add xmlrpctest
.Finish
-Start Tomcat Sever
.Right click Tomcat v6.0 at localhost->Start
-Run Simple Client
Right click SimpleClient.java->Run As->Java Application
Output in Console View:
Try to add 2 + 3 via XML-RPC... The returned values is: 5
