This post is to compile many way to access your EJB3 session/entity bean. Also You can use Singlton ServiceLocator(J2EE design pattern) to look up the EJB session bean from the JNDI Naming Server.
So your application flow would be something like this:
JSP (View) -> Servlet (Controller) -> Business Delegate (J2EE Design Pattern) -> Service Locator (J2EE Design Pattern) -> Session Bean (Business Tier)
=====================================================================
EJB3 session bean
=====================================================================
//session interface
package com.myapp;

import javax.ejb.Stateless;

public interface HelloEJB3 {
public String sayHello();
}

//session implementation
package com.myapp;

import java.ejb.Stateless;

@Stateless
@local
public class HelloEJB3Bean implements HelloEJB3 {
private String greeting = “Hello, EJB3!”;

public String sayHello() { return greeting; }
}

=====================================================================
Using Java EE component to call session bean
=====================================================================
/**
* servelt with EJB3 annotation to access session bean
*/
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;

public class HelloServlet1 extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
@EJB
private HelloEJB3 hellobean;

protected void sayHello() {
System.out.print.(hellobean.sayHello());
}
}

/**
* If you are using a standard Java EE component (Servlet, JSP, EJB, or Java EE Application Client),
* you will not need to set the properties explicitly when creating a JNDI InitialContext , no matter
* which EJB vendor you are using. That’s because the JNDI properties can be configured at deployment time
* and are applied automatically.
*/
public class HelloServlet2 extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

Context jndiContext = getInitialContext( );

HelloEJB3 hellobean = (HelloEJB3)jndiContext.lookup(“java:comp/env/ejb/HelloEJB3”);

protected void sayHello() {
System.out.print.(hellobean.sayHello());
}

public static Context getInitialContext( ) throws javax.naming.NamingException {
return new javax.naming.InitialContext( );
}
}

/**
* Once the session bean is deployed into the EJB 3.0 container, a stub object is created and
* it is registered in the server’s JDNI registry. The client code obtains a stub of the bean
* from the JNDI using its default JNDI name formatted as follows.
*
* If the application is deployed in a EAR file, the default JNDI name is
* the EAR-FILE-BASE-NAME/EJB-CLASS-NAME/local for the stub for local interface.
* For the remote interface, it is EAR-FILE-BASE-NAME/EJB-CLASS-NAME/remote.
*
* If the bean is deployed in a JAR file, the JNDI names are EJB-CLASS-NAME/local
* and EJB-CLASS-NAME/remote.
*/

/**
* JSF managed bean
*/
public class HelloMagBean {

Context jndiContext = getInitialContext( );

HelloEJB3 hellobean = (HelloEJB3)jndiContext.lookup(“MYEJB3Trail/HelloEJB3/local”);

protected void sayHello() {
System.out.print.(hellobean.sayHello());
}

public static Context getInitialContext( ) throws javax.naming.NamingException {
return new javax.naming.InitialContext( );
}
}

/**
* JSP initial page
*/
<%@ page import=”trail.slsb.*, javax.naming.*, java.text.*”%>

<%!
private HelloEJB3 hellobean = null;
public void jspInit () {
try {
InitialContext ctx = new InitialContext();
hellobean = (HelloEJB3) ctx.lookup(“MYEJB3Trail/HelloEJB3/local”);
} catch (Exception e) {
e.printStackTrace ();
}
}
%>

=====================================================================
stand alone client to access session bean such as swing application
=====================================================================
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;

public class HelloClientPOJO
{
HelloEJB3 hellobean;

public HelloClientPOJO() {
try{
Context jndiContext = getInitialContext( );
Object ref = jndiContext.lookup(“java:comp/env/ejb/HelloEJB3”);
hellobean = (HelloEJB3)ref;
} catch (javax.naming.NamingException ne){
//nothing
}
}

protected void sayHello() {
System.out.print.(hellobean.sayHello());
}

public static void main(String [] args)
{
HelloClientPOJO helloClient= new HelloClientPOJO();
helloClient.sayHello();

}

// developed for JBoss only. this is vender dependency
public static Context getInitialContext( ) throws javax.naming.NamingException {
Properties p = new Properties( );
p.put(Context.INITIAL_CONTEXT_FACTORY, “org.jnp.interfaces.NamingContextFactory”);
p.put(Context.URL_PKG_PREFIXES, ” org.jboss.naming:org.jnp.interfaces”);
p.put(Context.PROVIDER_URL, “jnp://localhost:1099″);
return new javax.naming.InitialContext(p);
}

}

=====================================================================
Spring bean to access session bean
=====================================================================
<bean id=”helloEJB3″ class=”org.springframework.jndi.JndiObjectFactoryBean”>
<property name=”jndiName” value=”java:comp/env/ejb/HelloEJB3″ />
<property name=”expectedType” value=”com.myapp.HelloEJB3″ />
</bean>

<bean id=”helloSpringPOJO” class=”com.nmetric.HelloSpringPOJO”>
<property name=”helloEJB3″ ref=”helloEJB3″/>
</bean>

public class HelloSpringPOJO
{
HelloEJB3 hellobean;

public HelloSpringPOJO() {}

protected void sayHello() {
System.out.print.(hellobean.sayHello());
}

public void setHelloEJB3(HelloEJB3 hellobean){
this.hellobean = hellobean;
}
}

References:
Using an EJB Session Bean as a Model Facade
Using Spring with EJB 3
Accessing objects in JNDI using Spring
JBoss EJB 3.0 and Extensions

Update 12-01-2009:
To use EntityManager in servlet and avoid thread race condition. Note, the request for servlet is to get a new EntityManger per thread.

1. How to get container-managed EntityManager in servlets
2. EntityManager in Servlets Doubt
3. Using ThreadLocal and Servlet Filters to cleanly access JPA an EntityManager

Advertisements