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.*;
@Remote
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.*;
@Stateless(mappedName=”calc”)
public class CalculatorBean implements CalculatorRemote
{
public int add(int x,int y)
{
return (x+y);
}
public int sub(int x,int y)
{
return(x-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

“index.jsp”

————————-

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

Step 5). Writing the Servlet

“MyServlet.java”————————–

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;

@EJB
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″>
<display-name>EJBWebClient</display-name>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>servlets.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>MyServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

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>
<wls:context-root>EJBWebClient</wls:context-root>
</wls:weblogic-web-app>

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

“EJBWebClient.war”

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.

.

.

Thanks

Jay SenSharma