工作中,需要用到JSON RPC,整了个小例子,并将实现过程贴出来,供新学者参考:至于JSON RPC的原理,在网上有很多文章,在此不再多言:
(1)获得JSON RPC支持,JSON RPC是一个客户端与服务器异步通讯机制,为实现JSON RPC,先得到JAR包,及其JS文件。也可以导入JSON RPC 源码到工程中。我在这现这个例子时,是导入了其源码。
(2)修改web.xml配置,当工程启动时,服务器加裁json rpc 相应的servlet ,获得容器支持,加入如下配置:
<!-- 用于JSON RPC -->
<servlet>
<servlet-name>JSONRPCServlet</servlet-name>
<servlet-class>
com.metaparadigm.jsonrpc.JSONRPCServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JSONRPCServlet</servlet-name>
<url-pattern>/JSON-RPC</url-pattern>
</servlet-mapping>
(3)创建后台类文件:此例中用到两个类 Examle.java,User.java
User.java类,如下:
package test;
import java.io.*;
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Example.java 类如下:
package test;
import java.io.*;
public class Example implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public String aboutMe(User user){
String str="您好,我是"+user.getName()+",今年"+user.getAge()+"岁";
return str;
}
public String aboutMe(){
String str="您好,我是张三,今年30岁";
return str;
}
}
(4)创建前台访问JSP文件,并编写相应的JS代码:jsonrpc.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<jsp:useBean id="JSONRPCBridge" scope="session"
class="com.metaparadigm.jsonrpc.JSONRPCBridge" />
<jsp:useBean id="example" scope="session"
class="test.Example" />
<%
JSONRPCBridge.registerObject("example", example);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'jsonrpc.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="js/util/jsonrpc.js"></script>
<script type="text/javascript">
<!--jsonrpc 客户端JS代码 -->
var jsonrpc=null;
function onLoad(){
jsonrpc=new JSONRpcClient("JSON-RPC");
}
function aboutme_1(){
var str1=jsonrpc.example.aboutMe();
alert(str1);
}
function aboutme_2(name,age){
var user={"javaclass":"test.User","name":name,"age":age};
var str2=jsonrpc.example.aboutMe(user);
alert(str2);
}
</script>
</head>
<body onload="onLoad()">
JSON RPC 测试页,通过JS异步获取远程服务器后台信息,在IE上显示。
<br>
<br>
异步调用后台数据,在前台显示
<input type=button id="aboutme_1" value=" show aboutme" onclick="aboutme_1()"><br><br>
通过客户端传入的信息,构造一个对象,传到后台,再在前台显示
<input type=button id="aboutme_2" value=" show aboutme" onclick="aboutme_2('李四','32')">
</body>
</html>
在JSP页面文件中,有几点,本人以为是比较重要的:
(1)通过useBean,载入JSONRPCBridge实例,并用同样的方式,载入业务处理类实例example.
<jsp:useBean id="JSONRPCBridge" scope="session"
class="com.metaparadigm.jsonrpc.JSONRPCBridge" />
<jsp:useBean id="example" scope="session"
class="test.Example" />
(2)将前面加裁的example注册到JSONRPCBridge
<%
JSONRPCBridge.registerObject("example", example);
%>
(3)引入Jsonrpc 的js <script type="text/javascript" src="js/util/jsonrpc.js"></script>
(4)在页面加载时new一个JSON RPC 客户端,这一步不可少
var jsonrpc=new JSONRpcClient("JSON-RPC");
(5)以上准备好后,在客户端就可以与服务器进行异步通讯了
posted on 2009-06-18 17:22
JINBO~~ 阅读(2673)
评论(1) 编辑 收藏