May 31, 2011

EJB3 Stateless Session Bean - Hello World Example

1) Open Eclipse. Go to File > New > Project ...
Select EJB > EJB Project
Name the Project as MyEJBProject. Click Next.
Ensure that "EJB Module" with "3.0" as the version & "Java" with "5.0" as the version, checkboxes are ticked. Click Next. Click Finish.

2) In the MyEJBProject, create the following pieces of code within the ejbModule folder

HelloUser.java interface


package info.icontraining.session;


import javax.ejb.Local;


@Local  
public interface HelloUser {
      public String sayHello(String name);
}


HelloUserBean.java class



package info.icontraining.session;


import javax.ejb.Stateless;


@Stateless
public class HelloUserBean implements HelloUser {

      public String sayHello(String name) {
           return "Hello " + name + "! Welcome to EJB 3!";
      }
}


3) In your web application project (this is a different project compared to the EJB project just created above), add the following servlet code and configure it in the web.xml file

HelloUserClient.java servlet class


package info.icontraining.ejb.client;


import javax.naming.*;
import info.icontraining.session.*;
import java.io.*;
import java.util.Hashtable;
import javax.servlet.ServletException;
import javax.servlet.http.*;


public class HelloUserClient extends HttpServlet {


   public void doGet(HttpServletRequest req, HttpServletResponse res) 
                   throws ServletException, IOException {

       Hashtable env = new Hashtable(); 
       env.put("java.naming.factory.initial", 
                       "org.jnp.interfaces.NamingContextFactory");
       env.put("java.naming.factory.url.pkgs", 
                       "org.jboss.naming:org.jnp.interfaces"); 
       env.put("java.naming.provider.url","localhost:1099"); 


       PrintWriter out = res.getWriter();

       Context context;
       try {
           context = new InitialContext(env);

           HelloUser helloUser = (HelloUser) context.lookup("MyEarApp/" 
+ HelloUserBean.class.getSimpleName() + "/local");

           out.println(helloUser.sayHello("Dinesh"));

       } catch (NamingException e) {
           e.printStackTrace();
       }
   }      
}


web.xml configuration


<servlet>
    <servlet-name>HelloUserClient</servlet-name>
    <servlet-class>info.icontraining.ejb.client.HelloUserClient</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>HelloUserClient</servlet-name>
    <url-pattern>/helloUserClient</url-pattern>
</servlet-mapping>


4) Right-click on the Web Application Project and select the "Properties" menu item.
Select "Java Build Path" from the options on the left.
Click on the "Projects" Tab.
Click the "Add..." button; Tick the "MyEJBProject" checkbox. Click OK. Again, click OK.
Any build errors in the Web Application project / servlet class will get removed.

5) Click on the "File" menu item at the top.
Select New > Project ...
Choose "J2EE" and then "Enterprise Application Project". Click Next.
Name the project as "MyEarApp". Click Next.
Check the "MyEJBProject" and the Web Application Project as the 2 modules to be added within the Enterprise Application Project. Also, check the "Generate Deployment Descriptor" option.
Click Finish.

Note: In Eclipse Helios, after creating the Enterprise Application Project, right click on the project, select Properties. In the pop-up window, select "Deployment Assembly" on the left. Then click on the "Add..." button and select choose Project and click Next. Select the 2 projects - the EJB Project (MyEJBProject) and the web application project together (by pressing down the CTRL key). Click Finish.

6) Open the application.xml file within the new created EAR project - MyEarApp - within the EarContent/META-INF folder.
Modify the <context-root> element of the web application to /MyEarApp

7) Deploy the MyEarApp project to the JBoss Application Server and test the EJB by typing the following URL in the browser:

http://localhost:8080/MyEarApp/helloUserClient

4 comments:

  1. simply awesome...gr8 job..keep up the good work :)

    ReplyDelete
  2. Thanks, I was looking for the working code

    ReplyDelete
  3. Great step by step solution.

    ReplyDelete