Jay SenSharma

Jay SenSharma

Here is my First Sample which  demonstrates that how we can use Dependency Injection(DI) in WebLogic Server.

Here in this sample we are going to develope a EJB3 Stateless Session Bean and then we will try to inject this bean inside a Servlet.

Step1). Writing the EJB Business Interface

package ejb3;
import javax.ejb.*;
public interface CalculatorRemote
public int add(int x,int y);
public int sub(int x,int y);
public int mult(int x,int y);
public int div(int x,int y);

Step2). Writing the EJB Bean class using @Stateless Annotation

package ejb3;
import javax.ejb.*;
public class CalculatorBean implements CalculatorRemote
public int add(int x,int y)
return (x+y);
public int sub(int x,int y)
public int mult(int x,int y)
return (x*y);
public int div(int x,int y)
return (x/y);

Step 3). Compiling the above classes using

C:TestDI/>javac -d . *.java

Now we can create a JAR file in the same location by adding the generated classes. (We need not to provide the “ejb-jar.xml”   or “weblogic-ejb-jar.xml”  because It is EJB3 and inside weblogic the EJB container will run the   weblogic.appc  to compile these EJBs to generate the stub and Skeletons.

C:TestDI/>jar   -cvf    EJBDI.jar    ejb3     *.java

Step 4). Now we need to write a Web Application where we can test the Dependency Injection



<%@ page contentType=”text/html;charset=UTF-8″%>
<html locale=”true”>
<body bgcolor=”white”>
<a href=”MyServlet”>Click Here To Check EJB3 Dependency Injection</a>

Step 5). Writing the Servlet


package servlets;
import java.io.*;
import javax.ejb.EJB;
import javax.servlet.http.*;
import javax.servlet.*;

import ejb3.CalculatorRemote;

public class MyServlet extends HttpServlet
private static final long serialVersionUID = 1L;

private CalculatorRemote injection;
public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
PrintWriter out=res.getWriter();
System.out.println(“nnt Injection  : “+injection);
out.println(“<h2> Injection  : “+injection);
out.println(“<h2> SUM of 100 and 200 : “+injection.add(100,200));
System.out.println(“<h2> SUM of 100 and 200 : “+injection.add(100,200));

Step 6). Writing “web.xml”

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns=”http://java.sun.com/xml/ns/javaee” xmlns:web=”http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd” xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd” version=”2.5″>

Step 7). Writing “weblogic.xml”

<?xml version=”1.0″ encoding=”UTF-8″?>
<wls:weblogic-web-app xmlns:wls=”http://www.bea.com/ns/weblogic/weblogic-web-app” 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 http://www.bea.com/ns/weblogic/weblogic-web-app http://www.bea.com/ns/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd“>
<wls:weblogic-version>Oracle WebLogic-10.3</wls:weblogic-version>

Step 8). Now Compile the Servlet Class and assemble the Deployment Descriptors inside a WAR file.


Step 9). Create an EAR File using META-INF/application.xml file .

Step 10). deploy EAR file(which contains both “EJBDI.jar” AND “EJBWebClient.war”) in WebLogic Server 10.3.




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.