Java 集成开发实例精解
源代码在线查看: catalogtestclient1.java~2~
package cmp2image; import javax.naming.*; import javax.rmi.PortableRemoteObject; import java.util.*; public class CatalogTestClient1 extends Object { private CatalogHome catalogHome = null; //Construct the EJB test client public CatalogTestClient1() { initialize(); } public void initialize() { try { //get naming context Context context = getInitialContext(); //look up jndi name Object ref = context.lookup("Catalog"); //look up jndi name and cast to Home interface catalogHome = (CatalogHome) PortableRemoteObject.narrow(ref, CatalogHome.class); Catalog catalog = catalogHome.create(); HashSet collection = catalog.findOne("AfricanOrchid"); Iterator i = collection.iterator(); Flower flower=null; System.out.println("Invento"); while (i.hasNext()) { String name = (String) i.next(); System.out.println("flowerName: " + name); } } catch(Exception e) { e.printStackTrace(); } } private Context getInitialContext() throws Exception { String url = "t3://zhb: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; } } //---------------------------------------------------------------------------- // Utility Methods //---------------------------------------------------------------------------- public CatalogHome getHome() { return catalogHome; } //Main method public static void main(String[] args) { CatalogTestClient1 client = new CatalogTestClient1(); // Use the getHome() method of the client object to call Home interface // methods that will return a Remote interface reference. Then // use that Remote interface reference to access the EJB. } }