?? csclient.java
字號:
package csejb;import javax.naming.*;import java.util.Properties;import javax.rmi.PortableRemoteObject;public class CsClient { private theServerHome theServerHomeObject = null; //創建一個訪問EJB的客戶端應用程序 public CsClient() { try { //得到名字上下文 Context ctx = getInitialContext(); //查詢jndi名 Object ref = ctx.lookup("theServer"); //通過強制轉型得到Home接口 theServerHomeObject = (theServerHome) PortableRemoteObject.narrow(ref, theServerHome.class); } catch(Exception e) { e.printStackTrace(); } } //這個函數返回一個經過初始化的上下文 private Context getInitialContext() throws Exception { String url = "t3://cgb-4wn01xj69v6:7001"; String user = null; String password = null; Properties properties = null; try { properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); properties.put(Context.PROVIDER_URL, url); if (user != null) { properties.put(Context.SECURITY_PRINCIPAL, user); properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password); } return new InitialContext(properties); } catch(Exception e) { System.out.println("Unable to connect to WebLogic server at " + url); System.out.println("Please make sure that the server is running."); throw e; } } //---------------------------------------------------------------------------- // 工具函數 //---------------------------------------------------------------------------- public theServerHome getHome() { return theServerHomeObject; } //Main函數 public static void main(String[] args) { String retStr="This is C/S EJB example.\n"; CsClient client = new CsClient(); //用client的getHome()函數調用Home接口函數得到遠程接口的引用 // 用遠程接口的引用訪問EJB try { retStr=retStr+"Now the Client application is ready to access the Server.\n"; theServer theServer=client.getHome().create(); retStr=retStr+theServer.CServer(); retStr=retStr+"This is client speaking.\nThe example is over!\n"; System.out.print(retStr); } catch(Exception e) { e.printStackTrace(); } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -