日期:2014-05-17 浏览次数:20466 次
public interface ServiceI { public String say(String name); public Errors showError(String info); }
public class FirstService implements ServiceI{ @Override public String say(String name) { String res = "Hello, "+name+" from PHPRPC"; return res; } public Errors showError(String info){ Errors er = null; er = new Errors(); er.setId("1111"); er.setMsg("phprpc make error:"+info); return er; } }
public class Errors implements Serializable{ private String id; private String msg; public Errors(){} public String getId() { return id; } public void setId(String id) { this.id = id; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
public class PhprpcGloabService extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { [b]PHPRPC_Server server = new PHPRPC_Server(); FirstService first = new FirstService(); server.add(first); server.start(req, res);[/b] } }
<!-- PHPRPC Web service发布 --> <servlet> <servlet-name>GloabService</servlet-name> <servlet-class> com.smartcoms.service.PhprpcGloabService </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>GloabService</servlet-name> <url-pattern>/rpcservice/*</url-pattern> </servlet-mapping>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="org.phprpc.*" %> <%@ page import="com.smartcoms.web.UIElement.Errors" %> <%@ page import="com.smartcoms.service.ServiceI" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Phprpc 调试</title> </head> <body> <% PHPRPC_Client client = new PHPRPC_Client("http://localhost:8082/smartcoms/rpcservice/GloabService"); ServiceI service = (ServiceI)client.useService(ServiceI.class); out.println(service.say("中国")); Errors er = service.showError("信息"); out.println(er.getId()); out.println(er.getMsg()); %> </body> </html>