将EJB部署到应用服务器中,然后通过其HOME INTERFACE 来调用BEAN中的方法

解决方案 »

  1.   

    try {
        Properties prop = new Properties();
        prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
        prop.put(Context.PROVIDER_URL, "t3://localhost:7001");
        Context ctx=new InitialContext(prop);
        Object objref = ctx.lookup ("Hello");
        HelloHome Hellohome = (HelloHome)PortableRemoteObject.narrow(objref, HelloHome.class);
        Hello Hellotest = Hellohome.create();
        //@todo
    } catch (Exception ex) {
        ex.printStackTrace();

      

  2.   

    itjourney(IT之旅) 已经写得很清楚了
    我帮他加个注释吧,try {
        Properties prop = new Properties();
        prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
        //配置你ejb服务器的地址和端口
        prop.put(Context.PROVIDER_URL, "t3://localhost:7001");
        Context ctx=new InitialContext(prop);
        //查找jndi名字,这里假设是Hello
        Object objref = ctx.lookup ("Hello");
        //建立home对象,XxxxHome就是 这里假设是ejb 是Hello 远程Home接口是XxxxHome
        HelloHome Hellohome = (HelloHome)PortableRemoteObject.narrow(objref, HelloHome.class);
        //用create方法得到远程接口 Hellotest(首字母应该小写比较规范)
        Hello Hellotest = Hellohome.create();
        //@todo把你要进行的下一步操作写在下面例如
        //Hellotest.killMyMachine();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
      

  3.   

    如果你会用一般的客户端调用EJB,那么Servlet的调用是差不多的(如果你还不会的话要好好补补功课了),区别在于如果Web容器如果和EJB容器在一个JVM里就可以用Local接口而不是Remote接口来提高性能,当然写法只有一点点区别,即是获得Home接口的不同
      

  4.   

    /*
    private ml.MLAdminHome getHome() throws NamingException {
    return (ml.MLAdminHome) getContext().lookup(ml.MLAdminHome.JNDI_NAME);
    } private InitialContext getContext() throws NamingException {
    Hashtable props = new Hashtable(); props.put(InitialContext.INITIAL_CONTEXT_FACTORY,
    "org.jnp.interfaces.NamingContextFactory");
    props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099"); // This establishes the security for authorization/authentication
    // props.put(InitialContext.SECURITY_PRINCIPAL,"username");
    // props.put(InitialContext.SECURITY_CREDENTIALS,"password"); InitialContext initialContext = new InitialContext(props);
    return initialContext;
    } public void testBean() { try {
    ml.MLAdmin myBean = getHome().create(); //--------------------------------------
    //This is the place you make your calls.
    System.out.println(myBean.getName("[email protected]")); } catch (RemoteException e) {
    e.printStackTrace();
    } catch (CreateException e) {
    e.printStackTrace();
    } catch (NamingException e) {
    e.printStackTrace();
    }
    }*/
    我用上面的方法在SERVLET中调用,提示有错误,说ml.MLAdminHome 这个符号不能被识别
    但是上面的方法在普通的客户端是可以的,各位大侠,这是为什么呀?
      

  5.   

    1 查找jndi name
    2 转换类型(Home)
    3 调用Home的方法