4 Replies Latest reply on Apr 8, 2002 9:04 AM by timd

    How do I import a deployed EJB into a JSP

    burnsanthony

      How do I import a deployed EJB into a JSP?

        • 1. Re: How do I import a deployed EJB into a JSP
          wchao

          You don't import an EJB into a JSP page. You need to import the remote (or local) interface, then perform a JNDI lookup to get the home (or local home) interface and use that to get the bean references with create or findBy{XXX}. Here's some sample code:

          <%@ page import=" javax.naming.InitialContext, javax.rmi.PortableRemoteObject, javax.ejb.*, com.mycompany.myproject.User, com.mycompany.myproject.UserHome" %>

          ... HTML code ...

          <%
          // Set up properties for JNDI
          Properties sysProps = System.getProperties();
          sysProps.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
          sysProps.setProperty("java.naming.provider.url", "localhost:1099");
          sysProps.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
          System.setProperties(sysProps);

          // Get a naming context
          InitialContext jndiContext = new InitialContext();

          // Get a reference to UserHome
          Object ref = jndiContext.lookup("myproject/UserHome");

          // Narrow reference and cast it to the Bean's Home interface
          UserHome userHome = (UserHome)PortableRemoteObject.narrow (ref, UserHome.class);

          String username = "burnsanthony";

          // Use home interface to find a User object by username
          User user = userHome.findByUsername(username);
          %>

          • 2. Re: How do I import a deployed EJB into a JSP
            burnsanthony

            thats what I've been trying to do but I cant seem to import the interface

            i get this error:
            Package org.jboss.docs.interest not found in import.
            import org.jboss.docs.interest.*;

            • 3. Re: How do I import a deployed EJB into a JSP
              wchao

              Make sure it's in your CLASSPATH.

              • 4. Re: How do I import a deployed EJB into a JSP
                timd

                How do you get a reference to a beans local home interface from a JSP? My JNDI-lookup fails from a JSP-page but succeeds from another EJB. Listing the available objects in the context shows, that the (local) bean is not available.