日期:2014-05-16 浏览次数:20535 次
package jcore.jsonrpc.rpcobj; //必须是jcore.jsonrpc.rpcobj包下才可以免注册 import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import jcore.jsonrpc.common.JsonRpcObject; import jcore.jsonrpc.entity.User; public class UserJSON extends JsonRpcObject implements java.io.Serializable{ private static final long serialVersionUID = 1L; /** * 直接返回普通类型 * */ public String helloJSONPRC(String userName){ //可从getAllInputParms获取页面值 //String userName = (String)this.getAllInputParms().get("userName"); return "Hello World "+userName +" !"; } /*** * 返回JavaBean类型 * */ public User getUser(){ return new User(1,"张三","123456",25); } public String saveUser(User user){ System.out.println("id: "+user.getUserId() + ",name:" + user.getUserName() + ",password: " + user.getUserPassword() + ",age: " + user.getAge()); return "保存用户成功!"; } /** * 返回List集合 * */ public List<User> getUserAll(){ List<User> users = new ArrayList<User>(); users.add(new User(1,"张三","123456",25)); users.add(new User(2,"李逵","123123",20)); return users; } /** * 返回Map集合 * */ public Map<String,User> getUserMapAll(){ Map<String,User> maps = new HashMap<String,User>(); maps.put("1",new User(1,"张三","123456",25)); maps.put("2",new User(2,"李逵","123123",20)); return maps; } }
package jcore.jsonrpc.entity; public class User implements java.io.Serializable{ private static final long serialVersionUID = 1L; private int userId; private String userName ; private String userPassword; private int age ; public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public User(int userId, String userName, String userPassword, int age) { super(); this.userId = userId; this.userName = userName; this.userPassword = userPassword; this.age = age; } /** * 必须注意:JSON-RPC必须要求你的JavaBean对象要用默认的无参构造函数 * */ public User(){ } }
<servlet> <servlet-name>JSONRPCServlet</servlet-name> <servlet-class> jcore.jsonrpc.servlet.JSONRPCServlet </servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>JSONRPCServlet</servlet-name> <url-pattern>/JRPC</url-pattern> </servlet-mapping>
<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/