Hi,

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")
@Local(ReplicableSFSLocalIntf.class)
public class ReplicableSFSBean mplements ReplicableSFSLocalIntf
{
int count = 0;
public ReplicableSFSBean()
{
System.out.println("nt ReplicableSFSBean-->  ReplicableSFSBean() invoked");
}

@Init("create")
public void initMethod() throws CreateException
{
System.out.println("nt ReplicableSFSBean-->  public void initMethod() invoked");
}

@Remove(retainIfException=true)
public void removeWithRetain() throws Exception
{
System.out.println("nt ReplicableSFSBean-->  removeWithRetain() invoked");
}

@Remove
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");
count=count+5;
return count;
}

public int testDecrement() throws Exception
{
System.out.println("nt ReplicableSFSBean --->  public int testDecrement()  invoked");
count=count-2;
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…

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

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

<%@ page import="java.util.*,javax.naming.*" %>
<%
Context env = (Context) new InitialContext();
try{
Object obj=env.lookup("java:comp/env/ejb/ReplicableSFSBean");
out.println("<BR><BR>obj=>"+obj.getClass()+"t"+obj);

sfsb3.ReplicableSFSLocalIntf  local=(sfsb3.ReplicableSFSLocalIntf )obj;
out.println("<BR><BR>local=>"+local.getClass()+"t"+local);

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

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

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

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<error-page>
<error-code>500</error-code>
<location>/error.html</location>
</error-page>

<ejb-local-ref>
<ejb-ref-name>ejb/ReplicableSFSBean</ejb-ref-name>
<local>sfsb3.ReplicableSFSLocalIntf</local>
</ejb-local-ref>

</web-app>

———————————-

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'>
<application>
<display-name></display-name>
<module>
<ejb>Test.jar</ejb>
</module>
<module>
<web>
<web-uri>WebApp1</web-uri>
<context-root>WebApp1</context-root>
</web>
</module>
</application>

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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

</webapp>

.

Regards
Jay SenSharma