您的位置:首页 > 编程语言 > Java开发

Eclipse开发XML-RPC程序——入门

2007-10-22 18:03 411 查看
最近想搞分布式系统,查了不少东东,才决定用JAVA,又决定先从XML-RPC开始着手。刚开始找了大半天的资料都是些类同或不适合入门者的文章,好晕。。。后来,改变了关键词,终于找到了一篇,真是柳暗花明又一村啊:)

开发流程如下:

Installing the development environment

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.

Install JDK 1.4

Install Apache Tomcat 4

Install Eclipse IDE

Install the Tomcat plugin for Eclipse (read the included readme.html!)


Download the Apache XML-RPC library

Start Eclipse

Under Window / Preferences / Tomcat, check Tomcat Version 4.1.x and browse to teh Tomcat installation directory; under Tomcat / JVM Settings select the JDK 1.4 or detected jvm in the JRE - dropdown.

If you are proficient with Tomcat, we recommend that you clean up all the extra stuff from
TOMCAT_HOME/conf/server.xml
as this will greatly decrease the startup time. (这个不清楚。。)

Creating a new Web-application project

To create a new web application, we must first set up a new project in Eclipse.

Start by creating a new Java Tomcat project from Eclipse: open File / New / Project and select Java / Tomcat project

Name the project, check "can update server.xml" and click Finish.

Right click the newly created project, select Properties / Tomcat, set the Context name to
/xmlrpc


Copy
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


In Eclipse, right click your project and choose Properties, under Java Build Path, on the Libraries-tab, and add the above mentioned file as an external JAR. In the adjoining Order and Export tab, check
xmlrpc-1.2-b1.jar.


In the Eclipse resource view, browse to the
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: