JSP call EJB in JBOSS?

Discussions

EJB programming & troubleshooting: JSP call EJB in JBOSS?

  1. JSP call EJB in JBOSS? (3 messages)

    Hello Everybody!
    I used JBOSS3.2.1 integrate Jetty and I want to write a JSP page that call CMP EJB. But I don't know how to config JBOSS to do this.
    Can anyone tell me step by step(because I'm a beginer J2EE) how to config JSP call EJB in JBOSS with Jetty.
    Thanks a lot!

    Threaded Messages (3)

  2. Hi
    First of all write your entity bean and make sure it is correct, then you should package your bean in jar file and put a deployment descritopr [ejb-jar.xml] for it, then you should bind your bean to jndi tree in jboss you can do this in jboss.xml, you should put it beside your ejb-jar.xml
    the syntax that you should use in jboss.xml is :
         <entity>
             <ejb-name>Instructor</ejb-name>
             <jndi-name>ejb/lms/Instructor</jndi-name>
             <local-jndi-name>ejb/lms/InstructorLocal</local-jndi-name>

          </entity>

    after that you should write your web tier code ,
    you should get an InitialContext object , and lookup your entity bean's jndi name . so at this point you have all the gates to your entiy . have fun now.
    if you have problems , let me know , i will give you more detailed instructions.
    --Amir Hossein
  3. Try plain java client first[ Go to top ]

    While you get up to speed on EJB authoring, you may prefer to write a test stub in plain Java that exercises your CMP EJB. Once that works, migrate the client code into your JSP. In other words break up your task into two discrete steps.

    Here is some code for a JBOSS3.2.1 CMP EJB lookup that you can use as a template:

    import javax.naming.*;
    import javax.rmi.PortableRemoteObject;
    import java.util.Hashtable;

    class ContactClient
    {

       public static void main(String[] args)
       {
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
    env.put(Context.PROVIDER_URL, "localhost:1099");
    env.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");

          // Enclosing the whole process in a single `try' block is not an ideal way
          // to do exception handling, but I don't want to clutter the program up
          // with catch blocks
          try
          {
             // Get a naming context
             InitialContext jndiContext = new InitialContext(env);
             System.out.println("Got context");
             
             // Get a reference to the Bean
             Object ref = jndiContext.lookup("Contact");
             System.out.println("Got reference");
             
             // Get a reference from this to the Bean's Home interface
             ContactHome home = (ContactHome)
             PortableRemoteObject.narrow(ref, ContactHome.class);
             
             // Create an object from the Home interface
             Contact contact= home.create();

    // Call remote methods
    contact.setName("Fred Blenkinsop");
    String retval = contact.getName();
    System.out.println(retval);
             
          }
          catch(Exception e)
          {
             System.out.println(e.toString());
          }
       }
    }
  4. Some problem need your help[ Go to top ]

    Thanks for your help!
    I still have a question.
    If I call EJB from client with JavaApp( not JSP) I must have some
    interface e.g: HomeInterface , a Interface which extends EJBObject.
    And I run my program successful.
    But JSP Page which call EJB is on server(run JBOSS with Jetty).
    Where do I put class interface in JBOSS for my JSP Page use it?
    Thank you very much!