Jay SenSharma

Jay SenSharma

Here is a very simple Step by Step instructions to develope a EJB3 Stateful Session Bean in WebLogic Server which allows us to perform a Local JNDI lookup.

Developing Local Lookup EJB3 in WebLogic 10.3 . . . .

This Sample demonstrates that how we can develop an EJB 3 Stateless Session Bean in WebLogic 10.3 by providing a custom Local JNDI Name.

Step 1). First of all we need to develop a EJB Local business interface “ReplicableSFSLocalInt.java” like following.

package sfsb3;
public interface ReplicableSFSLocalIntf
public void removeWithRetain() throws Exception;
public void removeWithoutRetain() throws Exception;
public String printBean ();
public int testIncrement() throws Exception;
public int testDecrement() throws Exception;
public int getCount() throws Exception;

Step 2). Now we are going to develop the EJB3 Stateless Session Bean class using @Stateful annotation.

package sfsb3;
import javax.ejb.*;
@Stateful(mappedName = "ejb/ReplicableSFSBean")
public class ReplicableSFSBean mplements ReplicableSFSLocalIntf
int count = 0;
public ReplicableSFSBean()
System.out.println("nt ReplicableSFSBean-->  ReplicableSFSBean() invoked");

public void initMethod() throws CreateException
System.out.println("nt ReplicableSFSBean-->  public void initMethod() invoked");

public void removeWithRetain() throws Exception
System.out.println("nt ReplicableSFSBean-->  removeWithRetain() invoked");

public void removeWithoutRetain() throws Exception
System.out.println("nt ReplicableSFSBean-->  removeWithoutRetain() invoked");

public String printRemoteIntf ()
System.out.println("nt ReplicableSFSBean --->  public String printRemoteIntf ()  invoked");
return "ReplicableSFSRemoteObjectIntf";

public String printLocalIntf ()
System.out.println("nt ReplicableSFSBean --->  public String printLocalIntf ()  invoked");
return "ReplicableSFSLocalObjectIntf";

public String printBean ()
System.out.println("nt ReplicableSFSBean --->  public String printBean ()  invoked");
return "ReplicableSFSBean";

public int testIncrement() throws Exception
System.out.println("nt ReplicableSFSBean ---> public int testIncrement()  invoked");
return count;

public int testDecrement() throws Exception
System.out.println("nt ReplicableSFSBean --->  public int testDecrement()  invoked");
return count;

public int getCount() throws Exception
System.out.println("nt ReplicableSFSBean --->  public int getCount()  invoked");
return count;

Step3).  We need to compile above two classes using

javac  –d  .  ReplicableSFSLocalIntf.java
javac  -d . ReplicableSFSBean.java

Step 4). We Need to Create a Jar file out of these two classes like below

jar  -cvf  Test.jar  sfsb3  *.java

Step 5). Now we need to develop a Web Application to invoke this EJB using its Local JNDI Name.

Developing “index.jsp” like following…

<head> <title>Test Local Lookup in WebLogic 10.3</title> </head>
<centre> <h2> Sample For EJB3 Local Lookup in WebLogic 10.3<br>
<h3><a href=first.jsp>Lookup for Local EJB3 Click Here</a>

Step 6). developing the “first.jsp” to write the Local Lookup code.

<%@ page import="java.util.*,javax.naming.*" %>
Context env = (Context) new InitialContext();
Object obj=env.lookup("java:comp/env/ejb/ReplicableSFSBean");

sfsb3.ReplicableSFSLocalIntf  local=(sfsb3.ReplicableSFSLocalIntf )obj;

out.println("<BR><BR>local testIncrement() =>"+local.testIncrement());
catch(Exception e)
out.println("<BR><BR>Exception => "+e);

EJB3 Stateless Session Bean invoked Successfully in WebLogic 10.3 Using Local Lookup.

Step7). Provide the “web.xml” like following:

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"





Step 8). Creating an EAR file with above Web Application and Test.jar EJB, using META-INF/application.xml  file like following

<!DOCTYPE application PUBLIC '-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN' 'http://java.sun.com/dtd/application_1_3.dtd'>

Step 9). Now deploy the EAR File on WebLogic Server and access the WebApp1 index.jsp page.


But if you are trying to inject an EJB  Bean inside your Servlet (WebApplication) using @EJB annotation then you must make sure that you are using the following link of “webxml”   which uses Servlet 2.5  XSD specification:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">



Jay SenSharma

If you enjoyed this post, please considerleaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.