ejb的client包括application、applet、jsp/servlet等等,也包括其他ejb。调用代码是一样的。

解决方案 »

  1.   

    在实施描述符中加入(在一个包中)时
    在调用bean的
    <enterprise-beans>
    <session(entity)>
    //加入
    <ejb-ref>
    <ejb-ref-name/>(被调bean的JNDI Name)(形式为ejb/名)
    <ejb-ref-type/>
    <home/>
    <remote/>
    <ejb-link/>(被调bean的ejb-name)
    </ejb-ref>
    </session(entity)>
    </enterprise-beans>
    调用时用查找EJB的方法就行
    (lookup("java:comp/env/ejb/名"));
    还有,你要是使用weblogic作为服务器的话还要在weblogic-ejb-jar.xml

    <weblogic-ejb-jar>
    //加入
    <weblogic-enterprise-bean>
    <ejb-name/>
    <jndi-name/>(注意与前面的一致)
    </weblogic-wnterprise-bean>
    <weblogic-enterprise-bean>
    <ejb-name/>
    <jndi-name/>(注意与前面的一致)
    </weblogic-wnterprise-bean>(把包中的所有bean都要按此形式写上)
    </weblogic-ejb-jar>
      

  2.   

    下面的例子,取自WebLogic下的bans.上面两位说的都对,我这里是示范如何在程序中调用。1。先找到EJB的Home.例子中要调的EJB是:MusicLibrary
    /**
       * Look up the MusicLibrary bean's home interface using JNDI.
       */
      private MusicLibraryHome lookupMusicLibraryHome()
        throws NamingException
      {
        Context ctx = getInitialContext();    try {
          Object home = 
            (MusicLibraryHome)ctx.lookup("MusicLibraryEJB.MusicLibraryHome");
          return (MusicLibraryHome)PortableRemoteObject.narrow(
            home,
            MusicLibraryHome.class);    } catch (NamingException ne) {
          log("The client was unable to lookup the EJBHome.  Please make sure " +
            "that you have deployed the ejb with the JNDI name " + 
            "MusicLibraryEJB.MusicLibraryHome on the WebLogic server at "+url);
          throw ne;
        }
      }  /**
       * Get an initial context into the JNDI tree.
       *
       * Java2 clients can use the jndi.properties file to set the
       * INITIAL_CONTEXT_FACTORY and the PROVIDER_URL
       *  private Context getInitialContext() throws NamingException {
       *    return new InitialContext();
       *  }
       *
       *
       * Using a Properties object will work on JDK 1.1.x and Java2
       * clients
       */
      private Context getInitialContext() throws NamingException {    try {
          // Get an InitialContext
          Properties h = new Properties();
          h.put(Context.INITIAL_CONTEXT_FACTORY,
            "weblogic.jndi.WLInitialContextFactory");
          h.put(Context.PROVIDER_URL, url);
          if (user != null) {
            log ("user: " + user);
            h.put(Context.SECURITY_PRINCIPAL, user);
            if (password == null) 
              password = "";
            h.put(Context.SECURITY_CREDENTIALS, password);
          } 
          return new InitialContext(h);
        } catch (NamingException ne) {
          log("We were unable to get a connection to the WebLogic server at "+url);
          log("Please make sure that the server is running.");
          throw ne;
        }
      }
    2。创建实例:(一句话而已,但前面要写对)
    MusicLibrary musicLib = musicLibraryHome.create();3。调用EJB的方法:
    // Remove any old data from the database.  
        musicLib.removeRecordingsThatExist(recInfos);
        musicLib.removeBandsThatExist(bandInfos);    // Create the Bands in the Database
        musicLib.addBands(bandInfos);
    我想可以了吧! 你的JNDI name不要搞错