Eclipse开发XML-RPC程序——入门
2007-10-22 18:03
411 查看
最近想搞分布式系统,查了不少东东,才决定用JAVA,又决定先从XML-RPC开始着手。刚开始找了大半天的资料都是些类同或不适合入门者的文章,好晕。。。后来,改变了关键词,终于找到了一篇,真是柳暗花明又一村啊:)
开发流程如下:
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
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
Copy
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
In the Eclipse resource view, browse to the
我用的是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>
开发流程如下:
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.xmlas 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/libdirectory, 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>
相关文章推荐
- 用Eclipse开发SWT程序配置入门
- 用eclipse开发hadoop入门程序wordcount
- Java入门学习-理解什么是JRE和JDK,然后搭建JAVA开发环境,用eclipse写出第一个helloworld程序
- 用Eclipse开发Java程序入门
- Eclipse开发android程序xml不提示
- [入门九]ADT和Eclipse开发Android程序时注意点
- Linux下用Eclipse+CDT开发fltk程序入门
- JavaFX入门(三):使用Eclipse开发JavaFX程序
- 报表引擎API开发入门—带参程序数据集
- 【学习笔记】零基础C#窗口程序开发入门
- 微信小程序开发入门(1)
- Eclipse+tomcat开发j2ee经典入门例子
- C#开发语音程序入门之语音识别
- eclipse开发Android程序时一些常见问题与错误提示
- ubuntu下使用eclipse c/c++开发arm linux程序
- 使用Eclipse开发ABAP程序
- 开发宝典:程序开发新手入门之Jave ME
- Android程序开发入门教程
- 智能小车28:openwrt程序开发入门