3 Replies Latest reply on Feb 14, 2012 3:22 AM by Wolfgang Knauf

    Enterprise application in Eclipse with Hibernate, ejb, servlet and jsp

    Eric Kwaku Akortse Newbie

      Hi,

      I would really love it if I could be given guidance to finish this project of mine. I have to create an application with EJB, servlet, jsp, and Hibernate for my database mapping. I cannot say I am very good with such an application development but I hope to understand any advice given me. Currently I am using eclipse indigo and Jboss 7 as my server and MySQL for my database. I have tried by trying to make a simple login with all the components above and if it works I continue from there. I am using he MVC development procedure. In that I have 3 different components. First the project name is RegistrationEAR and I have RegistrationEJB and RegistrationWAR components under this project. In all I have 3 different folders/directories. I have my jsp pages and my servlet under RegistrationWAR...I have my entity and session beans in my RegistrationEJB. My jsp has no problem communicating with my servlet but the problem is my servlet communicating with my EJB components in RegistrationEJB. Below is the tree structure of my project:

       

      tree.png

       

      RegistrationEJB

       

      regejb.png

       

      RegistrationWAR

       

      regwar.png

       

      Following are my codes:

       

      ControllerServlet.java

       

       

       

      package myejbclasses;
      
      
      
      
      import java.io.IOException;
      import java.util.Properties;
      //import java.util.Properties;
      //import javax.ejb.*;
      import javax.naming.*;
      //import javax.rmi.*;
      import javax.servlet.ServletException;
      import javax.servlet.annotation.WebServlet;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      
      
      
      
      /**
       * Servlet implementation class ControllerServlet
       */
      @WebServlet("/ControllerServlet")
      public class ControllerServlet extends HttpServlet {
                private static final long serialVersionUID = 1L;
      
          /**
           * @see HttpServlet#HttpServlet()
           */
          public ControllerServlet() {
              super();
              // TODO Auto-generated constructor stub
          }
      
      
                /**
                 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
                 */
          //@EJB
          //MyRemoteBusInterface remotebusinterface;
                protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                          // TODO Auto-generated method stub
      
                 // Step 4: Use JNDI to look up the EJB local home interface.
                    try {
      
                           Properties properties = new Properties();
                            properties.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
                            properties.put("java.naming.factory.url.pkgs","org.jboss.naming rg.jnp.interfaces");
                            properties.put("java.naming.provider.url", "jnp://localhost:8080");
      
                            Context ctx = new InitialContext(properties);
                            System.out.println("Got context");
                            MyRemoteBusInterface ans=(MyRemoteBusInterface) ctx.lookup("RegistrationEAR/MyStatelessSessBean/remote");
      
                            String name = request.getParameter("name");
                            if (name == null || name.length() == 0) {
                                name = "anonymous";
                            }
                            response.getWriter().write(ans.sayHello(name));
      
                     } catch (NamingException ne) {
                         System.out.println("Could not locate the bean.");
                     } catch (Exception e) {
                         // Unexpected exception; send back to client for now.
                         throw new ServletException(e);
                     }
                }
      
      
                /**
                 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
                 */
                protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                          // TODO Auto-generated method stub
                }
      
      
      }
      
      

       

       

      MyStatelessSessBean.java

       

      package myejbclasses;
      
      
      
      
      import javax.ejb.Stateless;
      
      
      @Stateless
      public class MyStatelessSessBean implements MyRemoteBusInterface {
      
      
                @Override
                public String sayHello(String name) {
                          // TODO Auto-generated method stub
                          //return getClass().getName() + " says hello to " + name + ".";
                          return " says hello to  you";
                }
      
      
      }
      
      

       

       

       

      MyRemoteBusInterface.java

       


      /**
       * 
       */
      package myejbclasses;
      
      
      import javax.ejb.Remote;
      
      
      /**
       * @author shengbelen
       *
       */
      @Remote
      public interface MyRemoteBusInterface {
                 public String sayHello(String name);
      }
      
      

       

       

      I guess this is all I need to provide for you analysis...If anything else is needed please let me know. Hope to hear something soon...Thank you in advance

        • 1. Re: Enterprise application in Eclipse with Hibernate, ejb, servlet and jsp
          Wolfgang Knauf Master

          Hi,

           

          your screenshot does not contain the EAR project, but I assume that you created an EAR project containing two modules (EJB and web project)?

           

          When doing a JNDI lookup inside your current server (web app is hosted by the same server as the EJB app), you don't need "Properties". Just create your InitialContext like this:

          InitialContext initialContext = new InitialContext();

           

          You might also use EJB injection instead of the JNDI lookup: add a member variable to your servlet and annotate it with "@EJB":

           

          @EJB

          private MyRemoteBusInterface myBean;

           

           

          Hope this helps

           

          Wolfgang

          • 2. Re: Enterprise application in Eclipse with Hibernate, ejb, servlet and jsp
            Eric Kwaku Akortse Newbie

            Hi Wolfgang,

             

                 thanks a lot for the response. I think I've tried this a couple of times but still does not work. And I think the EJB injections do not work with Jboss, thus, unless I am missing something. Yes I created the EAR project with those two modules.. So with all I said above I would be gratefull if you could enlighten me on the following questions:

             

            1. Does annotations work with Jboss? Hibernate or EJB?

            2. Would you suggest I use glassfish instead of Jboss since I read annotations work with it? (Though I would prefer to use Jboss because I'm used to that now)

             

            But all said and done I will still be working on your suggestions. Thanks again.

             

            eriakort.

            • 3. Re: Enterprise application in Eclipse with Hibernate, ejb, servlet and jsp
              Wolfgang Knauf Master

              Hi,

               

              basically, injection should work for JBoss. There must be some other trouble with your project, then ;-)

              Which exact JBoss version do you use? I would advice you to use 7.1.0CR1b, because the 7.0 branch was not feature complete.

               

              I never used the "@WebServlet" annotation to declare my servlet (I still did not find the time to dig deeper in the new JavaEE6 features ;-)), for me injection worked when declaring my servlet "the old way" in "web.xml". Maybe you could give this a try?

               

              If this does not help: try to post your EAR file (by "export to EAR file" including sources in Eclipse).

               

              Best regards

               

              Wolfgang