3 Replies Latest reply on Jun 28, 2003 11:15 AM by haraldgliebe

    Hello ! The problem of JNDI access through JSP !

    jonathan78wong

      Dear all ,
      Hello ! I create and deploy a jar in JBoss-3.2.1 (without Tomcat) . I have a JSP as follow :

      <%@ page
      session="false"
      isThreadSafe="true"
      isErrorPage="false"
      import="javax.naming.*, com.sample.*, javax.rmi.PortableRemoteObject, java.util.*, java.io.*, javax.net.*"
      %>

      <h4>test</h4>
      test
      <%
      try {

      Properties properties = new Properties();
      properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      properties.put(Context.PROVIDER_URL, "127.0.0.1:1099");
      properties.put(Context.URL_PKG_PREFIXES, "org.JBoss.naming:org.jnp.interfaces");

      InitialContext lContext = new InitialContext(properties);
      Object objref = lContext.lookup("HelloWorldRef");
      HelloWorldHome lHome = (HelloWorldHome) PortableRemoteObject.narrow(objref, HelloWorldHome.class);
      HelloWorld lSession = lHome.create();
      String fromEJB = lSession.hello();
      out.println( "" + fromEJB);
      }
      catch( Exception e ) {
      out.println( "Caugth exception: " + e.getMessage() );
      e.printStackTrace();
      }

      and the web.xml in :

      <?xml version="1.0" encoding="ISO-8859-1"?>

      <!DOCTYPE web-app
      PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

      <web-app>
      <display-name>Welcome to EJB</display-name>
      <ejb-ref>
      <ejb-ref-name>HelloWorld</ejb-ref-name>
      <ejb-ref-type>Session</ejb-ref-type>
      com.sample.HelloWorldHome
      come.samlpe.HelloWorld
      <ejb-link>HelloWorld</ejb-link>
      </ejb-ref>

      </web-app>

      and the jboss.xml in jar :

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE jboss-web
      PUBLIC "-//JBoss//DTD Web Application 2.3//EN"
      "http://www.jboss.org/j2ee/dtds/jboss-web_3_0.dtd">


      <enterprise-beans>

      <ejb-name>HelloWorld</ejb-name>
      <jndi-name>jb/HelloWorldHome</jndi-name>

      </enterprise-beans>


      and the ejb-jar.xml in jar :

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
      <ejb-jar>
      JBoss Hello World Application
      <display-name>Hello World EJB</display-name>
      <enterprise-beans>

      <ejb-name>HelloWorld</ejb-name>
      com.sample.HelloWorldHome
      com.sample.HelloWorld
      <ejb-class>com.sample.HelloWorldBean</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Container</transaction-type>

      </enterprise-beans>
      </ejb-jar>

      I place the web.xml in WEB-INF and EJBs in WEB-INF\classes

      However , Tomcat says :

      Caugth exception: HelloWorldRef not bound

      Therefore , what should I do to correct it ? Thank you !

        • 1. Re: Hello ! The problem of JNDI access through JSP !
          haraldgliebe

          Hi,

          if you use an ejb-ref to reference an EJB in a web application you have to prepend "java:comp/env/" to the ejb-ref-name. Therefore your lookup should be
          Object objref = lContext.lookup("java:comp/env/HelloWorldRef");
          You also shouldn't give any properties to the constructor of InitialContext when accessing "java:comp/env", since this namespace is managed by the container .


          Regards,
          Harald


          • 2. Re: Hello ! The problem of JNDI access through JSP !
            jonathan78wong

            Dear Harald ,
            Hello ! Thank you for your suggestion . I change my JSP as follow :

            <%@ page
            session="false"
            isThreadSafe="true"
            isErrorPage="false"
            import="javax.naming.*, com.sample.*, javax.rmi.PortableRemoteObject, java.util.*, java.io.*, javax.net.*"
            %>

            <h4>test</h4>
            test
            <%

            try {
            InitialContext lContext = new InitialContext();
            Object objref = lContext.lookup("java:comp/env/HelloWorldRef");
            HelloWorldHome lHome = (HelloWorldHome) PortableRemoteObject.narrow(objref, HelloWorldHome.class);
            HelloWorld lSession = lHome.create();
            String fromEJB = lSession.hello();
            out.println( "" + fromEJB);
            }
            catch( Exception e ) {
            out.println( "Caugth exception: " + e.getMessage() );
            e.printStackTrace();
            }
            %>

            And the jboss.xml in jar :

            <?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE jboss-web
            PUBLIC "-//JBoss//DTD Web Application 2.3//EN"
            "http://www.jboss.org/j2ee/dtds/jboss-web_3_0.dtd">


            <enterprise-beans>

            <ejb-name>HelloWorld</ejb-name>
            <jndi-name>HelloWorld</jndi-name>

            </enterprise-beans>


            And ejb-jar.xml in jar :

            <?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
            <ejb-jar>
            JBoss Hello World Application
            <display-name>Hello World EJB</display-name>
            <enterprise-beans>

            <ejb-name>HelloWorld</ejb-name>
            com.sample.HelloWorldHome
            com.sample.HelloWorld
            <ejb-class>com.sample.HelloWorldBean</ejb-class>
            <session-type>Stateless</session-type>
            <transaction-type>Container</transaction-type>

            </enterprise-beans>
            </ejb-jar>

            And web.xml in WEB-INF :

            <?xml version="1.0" encoding="ISO-8859-1"?>

            <!DOCTYPE web-app
            PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

            <web-app>
            <display-name>Welcome to EJB</display-name>
            <ejb-ref>
            <ejb-ref-name>HelloWorldRef</ejb-ref-name>
            <ejb-ref-type>Session</ejb-ref-type>
            com.sample.HelloWorldHome
            come.samlpe.HelloWorld
            <ejb-link>HelloWorld</ejb-link>
            </ejb-ref>

            </web-app>

            I redeployed the jar in JBoss-3.2.1 (without Tomcat) . However , the Tomcat says :

            Caugth exception: Name HelloWorld is not bound in this Context

            Therefore , what futher should I have . Thank you !

            • 3. Re: Hello ! The problem of JNDI access through JSP !
              haraldgliebe

              Oh, I didn't notice that you deploy your webapp on a standalone Tomcat. In that case it doesn't work with the ejb-name, since Tomcat can't see from the name of the ejb where it is deployed. I don't know if and how you could pass this information to tomcat.

              But you can lookup the bean directly from the jboss jndi. To do that specify the jndi.properties as in your first post to get an InitialContext for the jndi tree of jboss and then lookup the EJB with the name specified in the jboss.xml:


              Properties properties = new Properties();
              properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
              properties.put(Context.PROVIDER_URL, "127.0.0.1:1099");
              properties.put(Context.URL_PKG_PREFIXES, "org.JBoss.naming:org.jnp.interfaces");

              InitialContext lContext = new InitialContext(properties);
              Object objref = lContext.lookup("jb/HelloWorldHome");

              Regards,
              Harald