这里以jsp调用ejb为例,因为jsp与servlet其实是一会事,是jboss自带的template例子,运行环境是jboss3+tomcat4:1.代码级别调用:
try {
      Context lContext = new InitialContext();//创建容器环境
      TestSessionHome lHome = (TestSessionHome) lContext.lookup(
         "java:comp/env/ejb/webtest/TestSession"
      );//通过引用名来查找ejb的本地接口
      TestSession lSession = lHome.create();//创建ejb引用
      out.println( "" + lSession.getNewEntityId() );//调用ejb中的方法
   }
   catch( Exception e ) {
      out.println( "Caugth exception: " + e.getMessage() );
      e.printStackTrace();
   }2.部署(deployment)设置:
因为servlet与ejb都运行在容器中,通过容器来交流,因此,容器必须知道通过什么方式正确找到ejb,一般分这两步:
a)sun标准的web.xml部署描述:
<web-app> //根标签,指明本文件对一个web application进行部署描述
  <display-name>Web Client</display-name> 
- <!--  The Welcome File List 
  --> 
- <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>//default页面,没有关系 
  <welcome-file>index.html</welcome-file> 
  </welcome-file-list>
- <ejb-ref>
  <ejb-ref-name>ejb/webtest/TestSession</ejb-ref-name>//指明ejb引用别名 
  <ejb-ref-type>Session</ejb-ref-type> //指明ejb类型,session或者entity
  <home>test.interfaces.TestSessionHome</home> //指明ejb本地接口
  <remote>test.interfaces.TestSession</remote> //指明ejb远程接口
  </ejb-ref>
  </web-app>b)每个服务器容器虽然遵循了sun j2ee的规范,但是在实现上有差异,以jboss为例,为了获得ejb的实际jndi名称(又一个名字!!!),jboss在jboss-web.xml做了如下指定:<jboss-web>
   
   <ejb-ref>
      <ejb-ref-name>ejb/webtest/TestSession</ejb-ref-name>//ejb引用名,跟web.xml中的一致
      <jndi-name>ejb/test/TestSession</jndi-name>//jndi名称,容器唯一识别名
   </ejb-ref>
   
</jboss-web>c)那么,如何通过jndi名来知道实际的bean呢?jboss里头在ejb-jar.xml来进行了描述,记住,ejb-jar.xml是sun的标准文件:<ejb-name>test/TestSession</ejb-name>//ejb名称,又一个名称!!!         <home>test.interfaces.TestSessionHome</home>//实际的ejb内容
         <remote>test.interfaces.TestSession</remote>//同上
         <ejb-class>test.session.TestSessionBean</ejb-class>//同上
         <session-type>Stateful</session-type>
         <transaction-type>Container</transaction-type>那么,这个文件又怎么同jndi名字挂上钩呢?以下又是jboss自己的文件了--jboss.xml:
<session>
         <ejb-name>test/TestSession</ejb-name>//ejb名称,同ejb-jar.xml
         <jndi-name>ejb/test/TestSession</jndi-name>//jndi名称,ok,红军胜利会师
      </session>3.总结:
程序里通过context来调用,部署文件的引用关系是:应用别名-->jndi名-->ejb名-->ejb类和接口.4.建议:弄清楚这几个名字的含义和关系,你就可以掌握部署的核心了。

解决方案 »

  1.   

    我自己按照那个要求写了一下,代码如下:import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;
    import javax.naming.*;public class s1 extends HttpServlet {
      private static final String CONTENT_TYPE = "text/html; charset=GBK";
      private UserHome userHome;  //inite
      public void init(ServletConfig config) throws ServletException {
        super.init(config);
        try
        {
          String initCtxFactory=getInitParameter(Context.INITIAL_CONTEXT_FACTORY);
          String providerURL=getInitParameter(Context.PROVIDER_URL);
          Properties env=new Properties();
          env.put(Context.INITIAL_CONTEXT_FACTORY,initCtxFactory);
          env.put(Context.PROVIDER_URL,providerURL);
          Context ctx=new InitialContext(env);
          userHome=(UserHome)ctx.lookup("UserHome"); 
          }
          catch(Exception e){e.printStackTrace();}
      }
      //Process the HTTP Get request
      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();
        String name=null;
        String password=null;
        try
        {           
           out.println("hello,welcome to java world!!");     
           User user=(User)userHome.findByPrimaryKey("gongqh21");//问题就处在这里!??
           out.println(user.getPassword());
           out.println(user.getName());     }catch(Exception e){out.println(e.toString());}
      
      }  //Clean up resources
      public void destroy() 
     {
      }
    }用IE打开Servlets使显示如下:
    hello,welcome to java world!!  java.lang.NullPointerException 是不是我那个findByPrimaryKey("gongqh21");用错了啊?!请教!!!!!!!
      

  2.   

    userHome应该是空值,也就是userHome==null,所以出现了以上问题。看来你的配置还是有问题,你这里可以放置一些测试信息看看
    userHome=(UserHome)ctx.lookup("UserHome"); 
          }
          catch(Exception e){e.printStackTrace();}
    在异常里头打印信息,或者调用之前判断userHome有没有已经被赋值。应该好解决,呵呵。
      

  3.   

    的确有问题,输出如下-----------
    hello,welcome to java world!! 
    java.lang.NullPointerException //out.println(e.toString());java.lang.NullPointerException
      

  4.   

    我刚才又试了一下,问题的根源可能是在inite 语句块将userHome=(UserHome)ctx.lookup("UserHome") 从原来的try{}catch....分离开来
    结果两个都有异常!(原来的语句块和这个语句)
      

  5.   

    问题还没解决,在线等待哪位仁兄来救!  SOS
      

  6.   

    把代码发到我这边我看看吧,如果可以的话。[email protected]
      

  7.   

    运行servlets时会出现以下异常:D:\bea\jdk131_06\bin\javaw -classpath "D:\bea\weblogic700\server\lib\weblogic.jar;D:\bea\weblogic700\server\lib\webservices.jar"   -ms64m -mx64m -Djava.library.path=D:/bea/weblogic700/server/bin -Dbea.home=D:/bea -Dweblogic.Name=myserver -Djava.security.policy==D:/bea/weblogic700/server/lib/weblogic.policy -Dweblogic.management.discover=false -Dweblogic.ProductionModeEnabled=false -Dweblogic.management.username=gongqh21 -Dweblogic.management.password=gongqh21 weblogic.Server  
    Starting WebLogic Server...<2003-4-12 下午12时57分58秒> <Notice> <Management> <140005> <Loading configuration D:\bea\user_projects\mydomain\.\config.xml> <2003-4-12 下午12时58分06秒> <Error> <EmbeddedLDAP> <171516> <Could not get exclusive access to the embedded LDAP data files directory: .\myserver\ldap\ldapfiles> <2003-4-12 下午12时58分06秒> <Critical> <WebLogicServer> <000364> <Server failed during initialization. Exception:weblogic.server.ServiceFailureException: Could not get exclusive access to embedded LDAP data files due to existing serverweblogic.server.ServiceFailureException: Could not get exclusive access to embedded LDAP data files due to existing server at weblogic.ldap.EmbeddedLDAP.ensureExclusiveAccess(EmbeddedLDAP.java:899) at weblogic.ldap.EmbeddedLDAP.initialize(EmbeddedLDAP.java:204) at weblogic.t3.srvr.T3Srvr.initialize1(T3Srvr.java:717) at weblogic.t3.srvr.T3Srvr.initialize(T3Srvr.java:594) at weblogic.t3.srvr.T3Srvr.run(T3Srvr.java:282) at weblogic.Server.main(Server.java:32)> <2003-4-12 下午12时58分06秒> <Emergency> <WebLogicServer> <000342> <Unable to initialize the server: Fatal initialization exception
    Throwable: weblogic.server.ServiceFailureException: Could not get exclusive access to embedded LDAP data files due to existing server
    weblogic.server.ServiceFailureException: Could not get exclusive access to embedded LDAP data files due to existing server at weblogic.ldap.EmbeddedLDAP.ensureExclusiveAccess(EmbeddedLDAP.java:899) at weblogic.ldap.EmbeddedLDAP.initialize(EmbeddedLDAP.java:204) at weblogic.t3.srvr.T3Srvr.initialize1(T3Srvr.java:717) at weblogic.t3.srvr.T3Srvr.initialize(T3Srvr.java:594) at weblogic.t3.srvr.T3Srvr.run(T3Srvr.java:282) at weblogic.Server.main(Server.java:32)> ***************************************************************************The WebLogic Server did not start up properly.Exception raised: weblogic.server.ServiceFailureException: Could not get exclusive access to embedded LDAP data files due to existing server at weblogic.ldap.EmbeddedLDAP.ensureExclusiveAccess(EmbeddedLDAP.java:899) at weblogic.ldap.EmbeddedLDAP.initialize(EmbeddedLDAP.java:204) at weblogic.t3.srvr.T3Srvr.initialize1(T3Srvr.java:717) at weblogic.t3.srvr.T3Srvr.initialize(T3Srvr.java:594) at weblogic.t3.srvr.T3Srvr.run(T3Srvr.java:282) at weblogic.Server.main(Server.java:32)Reason: Fatal initialization exception
    Throwable: weblogic.server.ServiceFailureException: Could not get exclusive access to embedded LDAP data files due to existing server
    weblogic.server.ServiceFailureException: Could not get exclusive access to embedded LDAP data files due to existing server at weblogic.ldap.EmbeddedLDAP.ensureExclusiveAccess(EmbeddedLDAP.java:899) at weblogic.ldap.EmbeddedLDAP.initialize(EmbeddedLDAP.java:204) at weblogic.t3.srvr.T3Srvr.initialize1(T3Srvr.java:717) at weblogic.t3.srvr.T3Srvr.initialize(T3Srvr.java:594) at weblogic.t3.srvr.T3Srvr.run(T3Srvr.java:282) at weblogic.Server.main(Server.java:32)***************************************************************************
    请问这是什么原因?!该怎么处理?
      

  8.   

    public User findByPrimaryKey(pkclass pk)throws FinderException
    这里的参数是pkclass ?
    你用的是String...
    看一下.
    还有你的ejb到底怎么配置的,jndi name 是什么?
    发布成功了吗?