1 Reply Latest reply on Sep 9, 2004 6:08 PM by darranl

    JSP Access to Session Bean

    briana

      Can someone please confirm what is required to use a session Bean from a JSP, all deplayed on JBoss 3.2.5.

      I'm continually getting
      Error.javax.naming.NameNotFoundException: comp not bound

      I've got the default jndi.properties file in conf in the classpath.

      Also tried this method in the JSP:
      public static Context getInitialContext() throws javax.naming.NamingException {
      java.util.Properties p = new java.util.Properties();
      p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      p.put(Context.URL_PKG_PREFIXES, "jboss.naming:org.jnp.interfaces");
      p.put(Context.PROVIDER_URL, "localhost:1099");
      return new javax.naming.InitialContext(p);
      }

      Code segment in the JSP
      try {
      Context jndiContext = getInitialContext();
      Object ref = jndiContext.lookup("java:comp/env/ejb/UnwyAppHome");

      UnwyAppHome home = (UnwyAppHome)
      PortableRemoteObject.narrow(ref,UnwyAppHome.class);
      UnwyApp unwyApp = home.create();

      Then added a jboss.xml although I thought it was optional.
      jboss.xml
      <?xml version = '1.0' encoding = 'windows-1252'?>
      <!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 3.0//EN" "http://www.jboss.org/j2ee/dtd/jboss_3_0.dtd">

      <enterprise-beans>

      <ejb-name>UnwyAppBean</ejb-name>
      <jndi-name>UnwyAppHome</jndi-name>


      <ejb-name>UnwyBean</ejb-name>
      <jndi-name>UnwyLocalHome</jndi-name>

      </enterprise-beans>


      ejb-jar.xml
      <?xml version = '1.0' encoding = 'windows-1252'?>
      <!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>
      <enterprise-beans>

      Session Bean ( Stateful )
      <display-name>UnwyApp</display-name>
      <ejb-name>UnwyApp</ejb-name>
      unwy.model.UnwyAppHome
      unwy.model.UnwyApp
      <ejb-class>unwy.model.UnwyAppBean</ejb-class>
      <session-type>Stateful</session-type>
      <transaction-type>Container</transaction-type>
      <ejb-local-ref>
      <ejb-ref-name>ejb/local/Unwy</ejb-ref-name>
      <ejb-ref-type>Entity</ejb-ref-type>
      <local-home>unwy.model.UnwyLocalHome</local-home>
      unwy.model.UnwyLocal
      <ejb-link>Unwy</ejb-link>
      </ejb-local-ref>

      ....

        • 1. Re: JSP Access to Session Bean
          darranl

          Get rid of the 'java:comp/env/ejb/' in your lookup unless you have defined an ejb-ref in your web.xml

          Also get rid of setting properties for the InitialContext constructor, the default will be fine.

          Also what do you mean by the default jndi.properties is on the classpath, the server will have already managed that for you.