xml-rpc学习

(1)sever by servlet
(1.1)web.xml
<display-name>webdemo</display-name>
  <servlet>
    <servlet-name>XmlRpcServlet</servlet-name>
    <servlet-class>xmlrpc.client.servlet.XmlRpcServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>XmlRpcServlet</servlet-name>
    <url-pattern>/xmlrpc</url-pattern>
  </servlet-mapping>
</web-app>
(1.2)
a.
package xmlrpc.client.servlet;


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import xmlrpc.client.XmlRpcFacade;

public class XmlRpcServlet extends HttpServlet {
  private static final String CONTENT_TYPE = "text/html; charset=GBK";
  private XmlRpcFacade facade;
  public void init() throws ServletException {
      facade = new XmlRpcFacade();
  }

  public void doGet(HttpServletRequest request, HttpServletResponse response) throws
          ServletException, IOException {
      this.doService(request, response);
  }

  public void doPost(HttpServletRequest request, HttpServletResponse response) throws
          ServletException, IOException {
      this.doService(request, response);
  }

  public void doService(HttpServletRequest request,
                        HttpServletResponse response) throws ServletException,
          IOException {
      facade.execute(request, response);
  }

  public void destroy() {}
}


b.
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.xmlrpc.XmlRpcServer;

public class XmlRpcFacade {
    private static XmlRpcServer xmlrpc;
    static {
        xmlrpc = new XmlRpcServer();
        xmlrpc.addHandler("hello", new HelloHandler());
    }

    public void execute(HttpServletRequest request,
                        HttpServletResponse response) throws IOException {
        byte[] result = xmlrpc.execute(request.getInputStream());
        response.setContentType("text/xml; charset=GB2312");
        response.setContentLength(result.length);
        OutputStream out = response.getOutputStream();
        out.write(result);
        out.flush();
        out.close();
    }
}

sever by webserver
public class HelloServer {
    public HelloServer() {
    }
    public static void main(String[] args){
        if(args.length<1){
            System.out.println("Usage: java HelloServer [port]");
            System.exit(-1);
        }
        try{
//            XmlRpc.setDriver("org.apache.xerces.parsers.SAXParser");
            //start the server
            System.out.println("Starting XML-RPC Server......");
            WebServer server = new WebServer(Integer.parseInt(args[0]));
            //register our handler class
            server.addHandler("hello",new HelloHandler());
            server.start();

            System.out.println("Now accepting requests......");
        }catch(Exception e){
            System.out.println("Could not locate SAX Driver");
        }
    }
}
(2)client
import org.apache.xmlrpc.XmlRpc;
import org.apache.xmlrpc.XmlRpcClient;
import java.util.Vector;
import java.net.MalformedURLException;
import org.apache.xmlrpc.XmlRpcException;
import java.io.IOException;

/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2010</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class HelloClient {
    public HelloClient() {
    }
    public static void main(String[] args){
        if(args.length<1){
           System.out.println("Usage: java HelloClient [your name]");
           System.exit(-1);
       }
       try{
           //Use the Apache Xereces SAX Driver
//           XmlRpc.setDriver("org.apache.xerces.parsers.SAXParser");

           //server
           XmlRpcClient client = new XmlRpcClient("http://localhost:8585");
           //servlet
           //XmlRpcClient client = new XmlRpcClient("http://127.0.0.1:8080/webdemo/xmlrpc");
           //create request
           Vector params = new Vector();
           params.addElement(args[0]);

           //make a request and print the result
           String result = (String)client.execute("hello.sayHello",params);
           System.out.println("Response from server: "+ result);
       }catch(MalformedURLException e){
            System.out.println("Incorrect URL fro xml-rpc server foramt:"+e.getMessage());
        }catch(XmlRpcException e){
            System.out.println("XmlRpcException :"+e.getMessage());
        }catch(IOException e){
            System.out.println("IOException:"+e.getMessage());
        }

   }

}
(3)Handler
public class HelloHandler {
    public String sayHello(String name){
        System.out.println(name);
        return "Hello " + name;
    }
}
(4)jar
apache-xercesImpl.jar
commons-codec-1.3.jar
commons-httpclient-2.0.2.jar
xmlrpc-2.0.1.jar

你可能感兴趣的:(apache,html,Web,xml,servlet)