最近想搞分布式系统,查了不少东东,才决定用JAVA,又决定先从XML-RPC开始着手。刚开始找了大半天的资料都是些类同或不适合入门者的文章,好晕。。。后来,改变了关键词,终于找到了一篇,真是柳暗花明又一村啊:)
开发流程如下:
The first step is to install the development environment. In this example our development environment is based on Eclipse IDE and we use Tomcat servlet container. These tools integrate well and are freely available from their vendors.
TOMCAT_HOME/conf/server.xml
as this will greatly decrease the startup time. (这个不清楚。。) To create a new web application, we must first set up a new project in Eclipse.
/xmlrpc
xmlrpc-1.2-b1.jar
(from the xmlrpc-1.2-b1.zip archive) and place it under Eclipse/workspace/<your project name>/WEB-INF/lib
xmlrpc-1.2-b1.jar.
WEB-INF/lib
directory, right click and select Refresh 我用的是Apache XML-RPC 3。1,下面一个例子:
Server:
public interface ServicesHandler {
public String execute(String str);
}
public class HelloHandler implements ServicesHandler {
public String execute(String str){
return "Hello," + str + "!";
}
}
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import org.apache.xmlrpc.*;
import org.apache.xmlrpc.server.*;
import org.apache.xmlrpc.webserver.*;
public class XmlRpcServicesServlet extends HttpServlet {
private XmlRpcServletServer server;
public void init(ServletConfig pConfig) throws ServletException {
super.init(pConfig);
try {
// create a new XmlRpcServletServer object
server = new XmlRpcServletServer();
// set up handler mapping of XmlRpcServletServer object
PropertyHandlerMapping phm = new PropertyHandlerMapping();
phm.addHandler("HelloHandler", HelloHandler.class);
server.setHandlerMapping(phm);
// more config of XmlRpcServletServer object
XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl)server.getConfig();
serverConfig.setEnabledForExtensions(true);
serverConfig.setContentLengthOptional(false);
} catch (XmlRpcException e) {
try {
log("Failed to create XmlRpcServer: " + e.getMessage(), e);
} catch (Throwable ignore) {
}
throw new ServletException(e);
}
}
public void doPost(HttpServletRequest Request, HttpServletResponse Response)
throws IOException, ServletException {
server.execute(Request, Response);
}
public void doGet(HttpServletRequest Request, HttpServletResponse Response)
throws IOException, ServletException {
server.execute(Request, Response);
}
}
Client:
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Vector;
import java.net.URL;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
public class TestClient {
public static void main(String [] args) throws Exception {
try {
// config client
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL("http://localhost:8080/XmlRpc/HelloHandler")); // should be modified according to your configuration of jsp container
// create a new XmlRpcClient object and bind above config object with it
XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);
// create parameter list
Vector<String> params = new Vector<String>();
params.addElement("MY");
// execute XML-RPC call
String result = (String) client.execute("HelloHandler.execute", params);
System.out.println(result);
} catch (MalformedURLException e) {
System.out.println(e.toString());
} catch (XmlRpcException e) {
System.out.println(e.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
在WEB-INF下创建一个配置文件:web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<servlet>
<servlet-name>XmlRpcServer</servlet-name>
<servlet-class>XmlRpcServicesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>XmlRpcServer</servlet-name>
<url-pattern>/HelloHandler</url-pattern>
</servlet-mapping>
</web-app>