xml-rpc 2.0和3.0实例

xml-rpc 2.0介绍:
1、运行在不同操作系统、不同环境
2、使用http作为传输协议
3、xml作为传送信息的编码格式


xml-rpc是一种简单的,轻量级的通过HTTP协议进行RPC通信的规范。一个xml-rpc消息就是一个请求
体为xml的HTTP-POST请求,被调用的方法在服务器端执行并将执行结果以xml格式编码后返回。

xml-rpc 2.0实例:

服务端类

public class MyXmlRpcServer extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        XmlRpcServer xmlRpc=new XmlRpcServer();
        xmlRpc.addHandler("myHandler", new MyHandler());
        byte [] result=xmlRpc.execute(req.getInputStream());
        resp.setContentType("text/xml");
        resp.setContentLength(result.length);
        OutputStream outputStream=resp.getOutputStream();
        outputStream.write(result);
        outputStream.flush();
        outputStream.close();
    }  

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }

  
}
 



配置

<servlet>
        <servlet-name>MyXmlRpcServer</servlet-name>
        <servlet-class>xiu.rpc.MyXmlRpcServer</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyXmlRpcServer</servlet-name>
        <url-pattern>/MyXmlRpcServer</url-pattern>
    </servlet-mapping>
 



测试

public class MyXmlRpcClient {

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            XmlRpcClient xmlrpc = new XmlRpcClient("http://127.0.0.1:8080/rpc/MyXmlRpcServer");
            Vector params = new Vector();
            params.addElement("Tom");
            String result = (String) xmlrpc.execute("myHandler.sayHello",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();
        }

    }

}
 



rpc 3.0 写servlet

实例:

public class HelloHandler {

	public String sayHello(String str){
		return str;
	}
}
 

服务:

public class Server extends HttpServlet {

	private XmlRpcServletServer xmlRpc;
	
	public void init(ServletConfig config)throws ServletException{
		super.init(config);
		xmlRpc=new XmlRpcServletServer();
		PropertyHandlerMapping phm=new PropertyHandlerMapping();
		try {
			phm.addHandler("HelloHandler", xiu.com.HelloHandler.class);
			xmlRpc.setHandlerMapping(phm);
			XmlRpcServerConfigImpl serverConfig=(XmlRpcServerConfigImpl)xmlRpc.getConfig();
			serverConfig.setEnabledForExtensions(true);
			serverConfig.setContentLengthOptional(false);
		} catch (XmlRpcException e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		xmlRpc.execute(req, resp);
	}
	
	
}

 

测试:

public class xmlRpcClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		XmlRpcClientConfigImpl config=new XmlRpcClientConfigImpl();
		try {
			config.setServerURL(new URL("http://127.0.0.1:8080/rpc3/xmlrpc"));
			XmlRpcClient client=new XmlRpcClient();
			client.setConfig(config);
			Vector<String> param=new Vector<String>();
			param.add("tom");
			String result=(String) client.execute("HelloHandler.sayHello", param);
			System.out.println("result:"+result);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (XmlRpcException e) {
			e.printStackTrace();
		}
		
	}

}
 

 

<servlet>
        <servlet-name>XmlRpcServer</servlet-name>
        <servlet-class>xiu.rpc.Server</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>XmlRpcServer</servlet-name>
        <url-pattern>/sayHello</url-pattern>
    </servlet-mapping>
 


rpc 3.0 不写servlet,用属性文件来配置javabean

1、创建javabean

2、配置javabean

3、client调用

rpc 3.0 通过属性文件来配置服务,还是很方便的

 

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