4 Replies Latest reply on May 15, 2003 8:44 AM by Raja

    How JSP communicate with EJB placed on jboss

    vikaskansal Newbie

      Hi,
      I am new to this whole environment. I developed my first EJB by reading whole lot of stuff. and then I found this thread in which jonlee explains how to create ejb-jar.xml file and set up the datasource. I think I did everything in the way it supposed to be and then I deployed my beans into the jboss 3.2.1.i saw the deploy statement in the jboss console. Now the only thing which i didn't understand how the JSP know about my beans.I am using TOMCAT 4.1.
      I wrote this line on top of my page
      <%@ page import="com.a.b.c.*" %> where com.a.b.c is the name of the package i used for the EJB.
      so I am getting the package not found error. do I also have to copy the bean jar file into the lib of tomcat ?
      please help me .........

        • 1. Re: How JSP communicate with EJB placed on jboss
          vikaskansal Newbie

          can somebody help me ?

          • 2. Re: How JSP communicate with EJB placed on jboss
            Jon Barnett Master

            A remote client such as a servlet or a JSP communicates to an EJB through the EJB's interfaces - there is a home interface and the remote interface; an interface that declares the public methods of an EJB accessible by a client.

            These and the actual bean code are usually packaged together and deployed on the EJB container (JBoss) - by convention you would call it MyBean.jar. The client only needs the interface classes and you would package them in a jar called MyBean-client.jar which you put in the path of the client (in this case Tomcat, so it goes in the WEB-INF/lib of the web application's WAR. Use whatever convention you are comfortable with.

            So suppose I have a stateless session bean with a remote interface class like this:

            package com.amity.testbranch;


            public interface MyBean extends javax.ejb.EJBObject
            {
            public long serviceTest(long counter) throws java.rmi.RemoteException;
            }

            and a home interface class like this:

            package com.amity.testbranch;

            public interface MyBeanHome extends javax.ejb.EJBHome
            {
            MyBean create() throws java.rmi.RemoteException, javax.ejb.CreateException;
            }

            My JSP would have something like this:

            <%import="com.amity.testbranch.MyBeanHome,com.amity.testbranch.MyBean,..."%>

            Now you can do your JNDI search and so on:
            try
            {
            Properties jndiProps = new Properties();
            InitialContext jndiContext = new InitialContext(jndiProps);
            Object reference = jndiContext.lookup(jndiName);
            MyBeanHome home = (MenuManagerHome)PortableRemoteObject.narrow(reference, MyBeanHome.class);
            MyBean myBean = home.create();
            long value = myBean.serviceTest(1);
            }
            catch(Exception e)
            {
            ...

            Note that this only gives you a flavour for how to write the code, and this particular example assumes that you don't have to do anything special to set up the properties - for example, if the JVM already has the JBoss naming lookup properties configured.

            • 3. Re: How JSP communicate with EJB placed on jboss
              vikaskansal Newbie

              Thanks jonlee for your initial help.
              I did exactly the way you explain in your mail.
              Here is my JSP Code..
              <%@ page import="java.sql.*" %>
              <%@ page import="javax.sql.*" %>
              <%@ page import="javax.naming.*" %>
              <%@ page import="javax.ejb.*" %>
              <%@ page import= "javax.rmi.*" %>
              <%@ page import="com.cm4d.utility.dbutility.ejb.*" %>


              First EJB test


              <%

              try
              {
              InitialContext ctx = new InitialContext();
              out.println("got the context");
              Object obj = ctx.lookup("QMC/CM4D/Utility/RoutinesHome");
              out.println("search the lookup");
              RoutinesHome rHome = (RoutinesHome)
              PortableRemoteObject.narrow(
              obj, RoutinesHome.class);
              out.println("got the home interface");
              Routines rout = rHome.create();

              RowSet rset = rout.getRoutines();

              while(rset.next())
              {
              System.out.println(rset.getString("R_ID"));
              System.out.println(rset.getString("R_Label"));
              }

              }
              catch (javax.naming.NamingException e)
              {
              out.println(e.getMessage());
              }
              catch (Exception e) {
              System.out.print(e.getClass().getName() + ":");
              System.out.println(e.getMessage());
              }


              %>




              I am using out.println method to check the ourput for lines related with EJB.
              The JSP output is like this.
              got the context
              Name QMC is not bound in this Context
              so I understand that it created the initialcontext object but was not able to find the JNDI name..
              for creating the ejb-jar.xml and jboss.xml i used this forum posted by you
              http://www.jboss.org/modules/bb/index.html?module=bb&op=viewtopic&t= you please give me some idea about the problem ?

              • 4. Re: How JSP communicate with EJB placed on jboss
                Raja Master

                There is a problem with ur JNDI Name.

                Look up Javadocs for javax.naming.CompositeName for naming details. A Name like "ejb/HelloWorld" is treated as a composite name of {"ejb", "HelloWorld"}

                So your JNDI is possibly broken into a composite name of {"QMC", "CM4D","Utility", "RoutinesHome"}

                Try setting up a ejb-ref in your web.xml and use the jbossweb.xml to point your reference to the actual JNDI Name.

                Or Setup the EJB to not have any "/" in its JNDI Name(for a quick check, you can do this).

                HTH
                Raj