Topic: re: 一个简单的servlet CORBA client程序 |
Print this page |
1.re: 一个简单的servlet CORBA client程序 | Copy to clipboard |
Posted by: sothis Posted on: 2003-01-16 18:03 本文回答 http://www.chinajavaworld.net/forum/topic.cgi?forum=33&topic=268&show=0 下面是一个简单的servlet程序,该程序作为CORBA客户端存取一个CORBA服务器,返回服务器程序所在的主机的当前UNIX时间。如果想试一下该程序的话,可以使用BES5.1,使用BES所携带的J2SDK1.3.1_04。 public class Servlet2 extends HttpServlet { private org.omg.CORBA.ORB myOrb = null ; private servletclient.MyServices.TimeServer myServer = null; static final private String CONTENT_TYPE = "text/html; charset=GBK"; //Initialize global variables public void init() throws ServletException { if (myServer == null) { if (myOrb == null) { myOrb = org.omg.CORBA.ORB.init((String[])null, System.getProperties()); } myServer = servletclient.MyServices.TimeServerHelper.bind(myOrb, "/TimeServer_poa", "TimeServer".getBytes()); } } //Process the HTTP Get request public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head><title>Servlet2</title></head>"); out.println("<body>"); out.println("<p>The servlet has received a GET. This is the reply.</p>"); out.println(myServer.getTime()); out.println("</body></html>"); } //Clean up resources public void destroy() { } } 当然,别忘了跑SmartAgent(如果运行了BES那么smartAgent就起来了,否则需要运行osagent跑smartAgent)以及服务程序,下面是服务程序的代码 public class MyServicesServerApp { public static void main(String[] args) { try{ org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, System.getProperties()); POA poaRoot = POAHelper.narrow(orb.resolve_initial_references("RootPOA")); name = "TimeServer"; org.omg.CORBA.Policy[] TimeServerPolicies = { poaRoot.create_lifespan_policy(LifespanPolicyValue.PERSISTENT) }; POA poaTimeServer = poaRoot.create_POA(name + "_poa", poaRoot.the_POAManager(), TimeServerPolicies); poaTimeServer.activate_object_with_id(name.getBytes(), new TimeServerImpl()); poaRoot.the_POAManager().activate(); orb.run(); } catch(Exception ex) { System.err.println(ex); } } } 在C/S之间使用的IDL定义为 module MyServices { interface TimeServer { string getTime(); }; }; |
2.Re:一个简单的servlet CORBA client程序 [Re: sothis] | Copy to clipboard |
Posted by: sothis Posted on: 2003-01-16 18:36 忘了实现类代码 package servletclient.MyServices.server; import java.sql.*; import java.util.*; import java.math.*; import org.omg.PortableServer.*; public class TimeServerImpl extends servletclient.MyServices.TimeServerPOA { public String getTime() { return "The Time on Server is" + String.valueOf(System.currentTimeMillis()); } } |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |