使用jsonrpc

   JSON:JavaScript Object Notation,JavaScript对象的一种字面量描述格式,是一种轻量级的数据交换格式。
    RPC:Remote procedure call,远程过程(函数、方法)调用。
   jsonrpc 依赖 jsonrpc-1.0.jar 文件,以及一个 js文件(jsonrpc.js)
(1)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:applicationContext.xml
		</param-value>
	</context-param>

	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>

	<listener>
		<listener-class>
		<!--注册配置的Bean对象-->
	org.jsonrpc.service.RegistServiceToJsonrpcBridgeListener
		</listener-class>
	</listener>
	    
	<servlet>
        <servlet-name>JSONRPCServlet</servlet-name>
        <servlet-class>
            com.metaparadigm.jsonrpc.JSONRPCServlet
        </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>JSONRPCServlet</servlet-name>
        <url-pattern>/JSON-RPC</url-pattern>
    </servlet-mapping>

	<session-config>
		<session-timeout>120</session-timeout>
	</session-config>

	<welcome-file-list>
	  <welcome-file> index.html </welcome-file>
	</welcome-file-list>

</web-app>

(2)jsonrpc.xml
<?xml version="1.0" encoding="UTF-8"?>   
<beans xmlns="http://www.springframework.org/schema/beans"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd    
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd ">   
   
    <bean name="jsonrpcServiceConfigurer"   
        class="org.jsonrpc.service.spring.JsonrpcServiceConfigurer">   
        <property name="services">   
            <map>   
                <entry key="helloWorld">   
                    <ref bean="helloWorld" />   
                </entry>   
            </map>   
        </property>   
    </bean>   
</beans>

(3)applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd ">
    
    <import resource="jsonrpc.xml"/>
    
    <bean id="helloWorld"   
        class="com.logcd.service.impl.HelloWorldImpl">   
    </bean>
</beans>

(4)HelloWorld.java和HelloWorldImpl.java
public interface HelloWorld {
	public String sayHelloWorld(String name);
}

public class HelloWorldImpl implements HelloWorld{

	public String sayHelloWorld(String name) {
		return "Hello World! hello, " + name;
	}
}

(5)调用
<script type="text/javascript" src="script/jsonrpc.js"></script><script>
	var jsonrpc = null;
	//初始化JSONRpcClient对象
	jsonrpc = new JSONRpcClient("JSON-RPC");
	function jsonrpcHello(){
	    var name = "阿花!";  
	    var result = jsonrpc.helloWorld.sayHelloWorld(resultCallback,name); 
	}  
	function resultCallback(result, e){
		alert("The server replied: " + result);
	} 
</script>

你可能感兴趣的:(JavaScript,java,json,xml,Web)