Menu

Thursday, June 2, 2011

EJB3.0 - Stateless Session Bean and lookup in Servlet


Create EJB Project in NetBeans

1.       Create a EJB project. Go to File --> New Project --> Java EE --> EJB Module



2.       Enter name of the EJB project.


3.       Select the server. Here I have selected Glassfish and the Java EE version.

 5.       Now EJB Project is created.


Create a  Stateless Session Bean
1.       Enter name of the session bean.
2.       Select the package.
3.       Select the session bean type. I have selected Stateless session bean.
4.       Select remote / local interface.
5.       Click finish button.

6.       The stateless session bean and corresponding remote interface is created by the Net beans.

Declare methods in the remote interface.
The @Remote annotation tells the container that this is a Remote interface.

package com.ejb;
import javax.ejb.Remote;

@Remote
public interface EmployeeRemote {
    public String getEmpName();
}

Implement all the methods in the session bean.
1.       The bean should implement the remote / local interface.
2.       The @Stateless tell the container that this is a stateless bean.
3.       This stateless session bean is mapped to “ejb/EmployeeJNDI”. We can look up this bean in other programs.

package com.ejb;
import javax.ejb.Stateless;

@Stateless (mappedName="ejb/EmpJNDI")
public class Employee implements EmployeeRemote {

    public String getEmpName() {
        return "Akila";
    }
}

Deploy the project.
1.       Right click the project and click deploy.
2.       View the admin console of the glassfish server.
3.       The EJB is deployed successfully in the server.




Access the Stateless Session Bean in the servlet
1.       Create a web project.  File --> New Project --> Java Web -->  Web Application


2.       Enter name of the project.

3.       Select the server and click finish.
 

4.       Create a servlet class.


5.       To access the EJB, the EJB jar file should be in the class path of the web application.
6.       To add EJB jar – Go to MyWeb --> Properties à Libraries --> Add Project --> Select the EJB project --> select the EJB jar file.                                     

7.       Add the below code in your servlet class file.
          //Lookup the EJB from JNDI
           InitialContext ctx = new InitialContext();
           EmployeeRemote  empRemote = (EmployeeRemote)ctx.lookup("ejb/EmpJNDI");
           out.println ("Lookup success");              
           out.println (empRemote.getEmpName());
8.       Deploy the project.
9.       Run the project.

How to explicitly lookup EJB?

//Lookup the EJB from JNDI
InitialContext ctx = new InitialContext();
EJBBeanRemote remoteobj = (EJBBeanRemote)ctx.lookup("mappedname of ejb");

How to lookup By @EJB annotation?

@EJB (mappedName="ejb/EmpJNDI")
 private EmployeeRemote empRemote;

Here the container injects the instance of the remote EJB bean by the annotation.

How to lookup EJB by ejb-ref?

Add the EJB reference in the web.xml file
       empEJB
        com.ejb.EmployeeRemote
        ejb/EmployeeJNDI
In the servlet class, add the below code.

InitialContext ctx = new InitialContext();
EmployeeRemote empRemote = (EmployeeRemote) ctx.lookup("java:comp/env/empEJB");