Home

Who are we

Articles

SOAP - A Quick Intro to Webservices

XML-RPC - A Five-Minute Tutorial

Installing Apache2 and mod_perl (Windows XP)

Links

Untitled Document

A Java XML-RPC Server

Here's a standalone version of a Java server using the apaches xmlrpc (formerly known as Helma XML-RPC):

 
dateInfoServerJ.java
 
import org.apache.xmlrpc.WebServer;
import java.util.Date;
import java.text.MessageFormat;
import java.util.Vector;

public class dateInfoServerJ {

  public static void main(String[] args) {
    WebServer webserver = new WebServer(9000);
    webserver.addHandler("Date", new dateInfoServerJ());
    webserver.start();
//    System.out.println(dateInfoHandler.dateInfo("time"));
  }

  public Vector dateInfo(String option) {
    Object[] dateArgs = {new Date(System.currentTimeMillis())};
    Vector reply = new Vector();
    if (option.equals("date")) {
      reply.add(MessageFormat.format(
          "{0,date,MM/dd/yyyy}", dateArgs));

    } else if (option.equals("time")) {
      reply.add(MessageFormat.format(
          "{0,date,HH:mm:ss}", dateArgs));
    } else if (option.equals("all")) {
      reply.add(MessageFormat.format(
          "{0,date,MM/dd/yyyy}", dateArgs));
      reply.add(MessageFormat.format(
          "{0,date,HH:mm:ss}", dateArgs));
    } else {
      reply.add("Error - unrecognized option.");
    }
    return(reply);
   }
}

The stand-alone server in Java is not different from Perl and Python. The three lines

 
WebServer webserver = new WebServer(9000);
webserver.addHandler("Date", new dateInfoServerJ());
webserver.start();

create an instance of a web server, register the class Date with the server and then start the server.

Apache XML-RPC is not part of the standard Java installation, so you will need to download the package from apache and install (unzip) it somewhere and make sure it appears in your CLASSPATH (both, for compiling and running).

A Java XML-RPC Client

Finally, a Java XML-RPC client, again using Apache XML-RPC:

 
dateInfoClientJ.java
 
import org.apache.xmlrpc.XmlRpcClientLite;
import java.util.Vector;
import org.apache.xmlrpc.XmlRpcException;

public class dateInfoClientJ {
  private String serverURL = "http://localhost:9000/";

  public dateInfoClientJ(String option) {
    XmlRpcClientLite client = null;
    try {
      client = new XmlRpcClientLite(serverURL);
    } catch (Exception mue) {
      System.out.println("Error: Bad URL " + serverURL);
      System.exit(1);
    }
    Vector vOption = new Vector();
    vOption.add(option);
    java.lang.Object response = null;
    try {
      response = client.execute("Date.dateInfo", vOption);
    } catch (XmlRpcException xre) {
      System.out.println("Error: XmlRpcException");
    } catch (java.io.IOException ioe) {
      System.out.println("Error: IO Exception");
    }

    if (option.equals("date")) {
      Vector vResponse = (Vector)response;
      System.out.println("Current server date is " + 
			 (String)vResponse.elementAt(0));
    } else if (option.equals("time")) {
      Vector vResponse = (Vector)response;
      System.out.println("Current server time is " + 
			 (String)vResponse.elementAt(0));
    } else if (option.equals("all")) {
      Vector vResponse = (Vector)response;
      System.out.println("Current server date is " + 
			 (String)vResponse.elementAt(0));
      System.out.println("Current server time is " + 
			 (String)vResponse.elementAt(1));
    } else {
	Vector vResponse = (Vector)response;
	System.out.println("Response: " +
	  (String)vResponse.elementAt(0));
    }
  }

  public static void main(String[] args) {
    String option = args[0];
    dateInfoClientJ dateInfoClient1 = 
      new dateInfoClientJ(option);
  }
}

The main commands here are creating a client with new XmlRpcClientLite(), then making the request and capturing the response with client.execute().

Previous - XML-RPC Server and Client in Python