服务器端
package com.phprpc.test;
//接口定义
public interface IMessage {
public String say(String msg);
public User count(User user);
}
package com.phprpc.test;
//实现类
public class Message implements IMessage{
static int a = 0;
public String say(String msg){
System.out.println(++a);
return "Your message is:" +msg;
}
public User count(User user){
user.setCount(user.getCount()+1);
return user;
}
}
//此jsp为PHPRPC_Client client = new PHPRPC_Client("http://localhost:8080/phprpc/test.jsp"); 的test.jsp
<%@ page language="java" import="org.phprpc.*"%>
<%@ page language="java" import="com.phprpc.test.*"%>
<body>
<%
IMessage message = new Message();
PHPRPC_Server phprpc_server = new PHPRPC_Server();
phprpc_server.add("say",message);
phprpc_server.add("count",message);
System.out.println(phprpc_server.toString());
phprpc_server.start(request, response);
%>
</body>
//USER对象
package com.phprpc.test;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = -5227301031199193059L;
private int id;
private String name;
private int count;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//客户端
package com.phprpc.client;
import org.phprpc.PHPRPC_Client;
import com.phprpc.test.IMessage;
import com.phprpc.test.Message;
import com.phprpc.test.User;
public class ClientTest {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
PHPRPC_Client client = new PHPRPC_Client("http://localhost:8080/phprpc/test.jsp");
IMessage msg = (IMessage) client.useService(IMessage.class);
String a = msg.say("panxiuyan");
System.out.println(a);
User user = new User();
user.setId(1);
user.setName("panxiuyan");
user.setCount(0);
User u = msg.count(user);
System.out.println(u.getCount());
}
}
附件中的phprpc.jar是已编译的文件直接可以使用