jsonrpc的使用

注意:要事先将jsonrpc.jar和jsonrpc.js放到相关目录下

1.TestBean.java
 
package com.bean;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TestBean {
	
	public String sayHello(String name){
		return name+"你好!";
	}

	public  String citcleArea(double r,String unit){
		DecimalFormat dfmormat = new DecimalFormat("####.00");
		return dfmormat.format(r*r*Math.PI)+"平方"+unit;
	}
	
	public String serviceDate(){
		Date date = new Date();
		SimpleDateFormat sfd = new SimpleDateFormat("yyyy年mm月dd hh:mm:ss");
		return sfd.format(date).toString();
	}
	
	public String[][] stringArray(int row,int col){
		
		String[][] array = new String[row][col];
		for(int i = 0 ; i <row;i++){
			for(int j  = 0 ; j < col; j ++){
				array[i][j] = "第"+(i+1)+"行,第"+(j+1)+"列";
			}
		}
		return array;
	}
}


2.index.jsp
 
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<jsp:useBean id="JSONRPCBridge" scope="session"
	class="com.metaparadigm.jsonrpc.JSONRPCBridge" />
<jsp:useBean id="testBean" class="com.bean.TestBean" />
<%
  JSONRPCBridge.registerObject("testBean",testBean);
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>My JSP 'index.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">
		<script type="text/javascript" src="jsonrpc.js">
</script>
		<script type="text/javascript">
jsonrpc = new JSONRpcClient("JSON-RPC");

function callServer() {
	myName = document.getElementById("myName").value;
	if (myName == "") {
		window.alert("请输入姓名!");
		return;
	}
	window.alert(jsonrpc.testBean.sayHello(myName));
}

function callc() {
	var radius = document.getElementById("radius").value;
	var unit = document.getElementById("unit").value;
	if (radius == "") {
		window.alert("请输入半径!");
		return;
	}
	if (unit == "") {
		window.alert("请输入单位!");
		return;
	}

	var result = jsonrpc.testBean.citcleArea(radius, unit);
	document.getElementById("area").value = result;
}

function getNow() {
	var result = jsonrpc.testBean.serviceDate();
	document.getElementById("now").value = result;
}

function createTable() {
	var rows = document.getElementById("rows").value;
	var columns = document.getElementById("columns").value;
	if (rows == "") {
		window.alert("请输入行数!");
		return;
	}
	if (columns == "") {
		window.alert("请输入列数!");
		return;
	}
	var result = jsonrpc.testBean.stringArray(rows, columns);
	for ( var i = 0; i < result.length; i++) {
		tableRow = document.getElementById("table1").insertRow(-1);
		for ( var j = 0; j < result[i].length; j++) {
			tableCell = tableRow.insertCell(-1);
			tableCell.innerHTML = result[i][j];
		}
	}
}
</script>
	</head>
	<h2>
		json-rpc实例
	</h2>
	<hr>
	请输入姓名:
	<input type="text" id="myName">
	<input type="button" value="问好" onclick="callServer();">
	<br>
	<br>

	请输入圆的半径:
	<input type="text" id="radius" size="6">
	单位:
	<input type="text" id="unit" size="4">
	面积为:
	<input type="text" id="area" readonly="true" size="12">
	<input type="button" value="计算" onClick="callc()">
	<br>
	<br>

	服务器当前时间:
	<input type="text" id="now" readonly="true" size="30">
	<input type="button" value="取得时间" onClick="getNow()">
	<br>
	<br>

	请输入表格的行列数:
	<input type="text" id="rows" size="4">
	行
	<input type="text" id="columns" size="4">
	列
	<input type="button" value="生成表格" onClick="createTable()">
	<br>
	<table id="table1" border="1" width="500" bgcolor="#eeeeee"></table>
	<br>

	<body>
	</body>
</html>


3.web.xml
 
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <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>
</web-app>

你可能感兴趣的:(json)