First, preparation process
The remote process calls RPC, based on XML transmission mode, of course, the low-level API, don't worry, but must have the software still have, first give a list of list jdk1.4.2 Don't say Xerces parsers to http: // XML .apache.org / go to download, XML-RPC development kit, http://ws.apache.org/xmlrpc/ can be obtained
Place all of the above JARs to the development environment in the ClassPath.
Second, Hello World
XML-RPC If you want to run, you need four components, web server, server classes, processing classes, customer classes
Web Server.
There is a lightweight web server in the XML-RPC package we have downloaded. In the program, we only need to simply use the following statements to start. / / Establish an object to transmit a port webserver server = new Web Server ("8989")); // Start Server.Start ();
2. Write the processing class
Handling the remote classes in the RMI, but there is no need to interface here.
In this class, one or more public methods are included for remote clients to call.
Public class hellohandler {
Public String Sayhello (String Name) {Return "Hello" Name;}
}
3. Server
Responsible to call the above code to activate the server, but also bind the remote object to the server.
Import java.io.ioException; // Introduce must be package, of course your XML-RPC package should be import org.apache.xmlrpc.webserver in classpath; import org.apache.xmlrpc.xmlrpc; public class helloserver {/ * * Main method * / public static void main (String [] args) {try {// uses Xerces XML parser XMLRPC.SetDriver ("org.apache.pars.Pars.SAXPARSER"); / / give a prompt, and Start the server System.out.Println ("Starting XML-RPC Server ...") on the end 8989; WebServer Server = New Web Server ("8989"); server.Start (); // Put HelloHandler The instance of the class is entered on the web server, and hello is the identifier of the processing class. When the client is called to get server.addhandler ("Hello", new hellohandler ()); system.out.println ("Registered HelloHandler Class TO / "Hello /");} catch (classnotfoundexception e) {system.out.println ("COULD NOT LOCATE SAX DRIVER);} catch (exception e) {system.out.println (" Could NOT START Server: " E.GetMessage ());}}} 4. The client is locked according to the" Identification Name. Method Name "to locate the remote processing method.
Import java.io.ioException; import java.net.malformedurlexception; import java.util.Vector;
// Import must be import org.apache.xmlrpc.xmlrpc; import org.apache.xmlrpc.xmlrpcclient; import org.apache.xmlrpc.xmlrpcexception;
Public class helloclient {
Public static void main (string args []) {string Yourname = "liu xiaobai"; try {// uses Apache Xerces SAX parser XMLRPC.SetDriver ("org.apache.parsrs.saxparser"); // Location remote Server, http: // host address: port number, 8989 is the port XMLRPCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCLIENT ("http:// localhost: 8989 /"); // Create a call request, method list of parameters If a vector object is stored. Vector params = new vector (); params.addelement (youRName); // issued a request, and returned the result, Execute requires two parameters, the first parameter "ID. Method name", the second parameter is a just established Vector object string result = (string) Client.execute ("Hello.sayhello", params; system.out.println ("Response from Server:" Result;
} Catch (ClassNotFoundException e) {System.out.println ( "Could not locate SAX Driver");} catch (MalformedURLException e) {System.out.println ( "Incorrect URL for XML-RPC server format:" e.getMessage ());} Catch (xmlrpcexception e) {system.out.println ("XML-RPC Exception:" E.GetMessage ());} catch (ioException e) {system.out.println ("IO Exception:" E.getMessage ());}}}
5, compile the above code, make sure the parser, the XMP-RPC development package of the JAR file in the classpath.
Run server
Java HelloServer
Run client
Java HelloClient
6. A universal XML server
Function Description: Configure the processor to be loaded by configuration files.
1. Profile name and location
Name: config.properties
In the root directory of the class file, if you are the default package, then the same directory; if the package name is com.hello, then the file should be placed in the same level as the COM directory.
content:
# 标 名 名 = Class name hello = javaxml2.hellohandler
2. Universal source code
import java.io *;. import org.apache.xmlrpc *;. import java.util.Properties; import java.util.Enumeration; import java.util.Hashtable; public class MyLightXMLServer {private WebServer server; private int port; private String configfile; public MyLightXMLServer (int port, String config) {this.port = port; this.configfile = config;} // start the server public void start () throws IOException, ClassNotFoundException, Exception {XmlRpc.setDriver ( "org.apache .XERCES.PARSERS.SAXPARSER "); System.out.Println (" Starting Up XML-RPC Server ... "); Server = New Web Server (port); // Call the registration function registerHandlers (this.getHandlers ()); server.start ();} public void registerHandlers (Properties handlers) throws Exception {Enumeration enum = handlers.propertyNames (); while (enum.hasMoreElements ()) {String temp = (String) enum.nextElement (); String tempcls = (String); Class CLS = Class.Forname (TempCLS); Server.AddHandler (Temp, CLS.NEW Instance ());}} public Properties getHandlers () {try {Properties properties = new Properties (); properties.load (new FileInputStream (new File ( "config.properties"))); return properties;} catch (Exception e ) {e.printStackTrace ();} return null;} public static void main (String args []) {String port = "8989"; String configfile = ""; MyLightXMLServer server = new MyLightXMLServer (Integer.parseInt (port), CONFIGFILE); try {server.start ();} catch (exception e) {E.PrintStackTrace ();}}}
After compiling MYLIGHTXMLSERVER .JAVA and runs, the server loads the processor in the configuration file and then directly execute HelloClient.
Java HelloClient