developing a Session Bean and a Servlet and deploy the web application on JBoss 3.0

developing a Session Bean and a Servlet and deploy the web application on JBoss 3.0 Writing Calculator Session Bean and Calling through JSP Previous Tutorial Index Next In this lesson I will show you how to develop a Calculator Stateless Session

developing a Session Bean and a Servlet and deploy the web application on JBoss 3.0

Welcome to the Jboss 3.0 Tutorial

     

In this lesson I will show you how to develop a Calculator Stateless Session Bean and call it through JSP file  and deploy the web application on JBoss 3.0 Server. This example shows you how to write deployment descriptor for two session beans. In this Lesson we will include the MyTestSession Session Bean developed in Lesson 3. Infact we will use the same development directory structure and add and modify the required file.

So first create our Calculator Session Bean write the deployment descriptor.

Writing Calculator Session Bean

Enterprise Bean remote interface

All remote interfaces must extend javax.ejb.EJBObject. Remote interface is the client view of session bean. Methods defined in the remote interface are accessible to the client. In our example we have defined the 

  public int add(int a, int b) 
  public int divide(int a, int b)  and
  public int multiply(int a, int b) 
 

method for calling from JSP. Here is code of our Remote Interface:

package calculator.session;

/**
* @author Deepak Kumar
* @Web http://www.roseindia.net
* @Email [email protected]
*/

import java.lang.*;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

public interface calculatorRemote extends javax.ejb.EJBObject{

public int add(int a, int b) throws RemoteException;
public int divide(int a, int b) throws RemoteException;
public int multiply(int a, int b) throws RemoteException;


}  

Enterprise Bean Home interface

All home interfaces must extend javax.ejb.EJBHome. 'create()' method of home interface of the application enables the client to create and remove the session object. 

package calculator.session;

/**
* @author Deepak Kumar
* @Web http://www.roseindia.net
* @Email [email protected]
*/

import javax.ejb.*;
import java.rmi.RemoteException;
import java.lang.*;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

public interface calculatorHome extends EJBHome{

public calculatorRemote create() throws RemoteException, CreateException;

}  

Enterprise Bean class

All Bean class are defined as public and implements the javax.ejb.SessionBean. In the bean class we have implemented the code for

  public int add(int a, int b) 
  public int divide(int a, int b)  and
  public int multiply(int a, int b) 
 

Besides this method other required methods which is to be implemented are:

  1. ejbCreate()

  2. ejbRemove()

  3. ejbActivate()

  4. ejbPassivate()

  5. setSessionContext(SessionContext aContext)

Here is the code for our Calculator Session Bean:


/*
* @author Deepak Kumar
* @Web http://www.roseindia.net
* @Email [email protected]
*/

package calculator.session;

import javax.ejb.*;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;


public class calculatorBean implements SessionBean{

public void ejbCreate() throws CreateException{
}

public void setSessionContext(SessionContext ctx) throws EJBException{
}

public void ejbRemove() throws EJBException{
}
public void ejbActivate() throws EJBException{
}

public void ejbPassivate() throws EJBException{
}


public int add(int a, int b){
System.out.println("From add function!");
return a+b;
}

public int divide(int a, int b){
System.out.println("From divide function!");
return a/b;
}

public int multiply(int a, int b){
System.out.println("From multiply function!");
return a*b;
}
}  

Jar Descriptor File

For creating example.jar ejb-jar.xml and jboss.xml files are required which explains the content of jar file.

ejb-jar.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">

<ejb-jar>

<description>Example 3</description>
<display-name>Example 3</display-name>

<enterprise-beans>

<!-- Session Beans -->
<session id="test_MyTestSession">
<display-name>My Test Session Bean</display-name>
<ejb-name>test/MyTestSession</ejb-name>
<home>test.session.MyTestSessionHome</home>
<remote>test.session.MyTestSession</remote>
<ejb-class>test.session.MyTestSessionBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>


<session id="Calculator">
<display-name>Calculator Session Bean</display-name>
<ejb-name>ejb/CalculatorSessionBean</ejb-name>
<home>calculator.session.calculatorHome</home>
<remote>calculator.session.calculatorRemote</remote>
<ejb-class>calculator.session.calculatorBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>



</enterprise-beans>

<assembly-descriptor>

</assembly-descriptor>

</ejb-jar>

  

Above deployment descriptor defines remote, home and bean class for the bean and assigns a name 'ejb/CalculatorSessionBean' to the session bean. Please note that bean of Stateless type and is defined by:

<session-type>Stateless</session-type>

jboss.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS//EN" "http://www.jboss.org/j2ee/dtd/jboss.dtd">

<jboss>

<enterprise-beans>

<session>
<ejb-name>test/MyTestSession</ejb-name>
<jndi-name>ejb/test/MyTestSessionBean</jndi-name>
</session>

<session>
<ejb-name>ejb/CalculatorSessionBean</ejb-name>
<jndi-name>ejb/CalculatorSessionBean</jndi-name>
</session>





</enterprise-beans>

<resource-managers>
</resource-managers>

</jboss>

The jboss deployment descriptor assigns jndi name ' ejb/CalculatorSessionBean' to the 'ejb/CalculatorSessionBean' bean.

Writing JSP and Web/Ear component

Our JSP file access the session bean and uses it for the calculation and displays the result.

For this purpose we are using add.jsp which displays a form to accept two numbers from the user and submits the form data to calculator.jsp. Here is our add.jsp:

<%@page language="java" %>
<html>

<head>
<title>Welcome to Jboss 3.0 tutorial</title>
<script language="javascript">
function validate(){
if (window.document.addform.number1.value.length==0){
alert("Please enter number1 !");
window.document.addform.number1.focus();
return false;
}
if (window.document.addform.number2.value.length==0){
alert("Please enter number2 !");
window.document.addform.number2.focus();
return false;
}

}
</script>

</head>

<body bgcolor="#FFFFCC">

<p align="center"><font size="6" color="#800000">
Jboss 3.0 Tutorial<br>
Lesson 4</font></p>
<form name="addform" action="calculator.jsp" onsubmit="return validate();" method="post">
<div align="center">
<center>
<table border="1" cellpadding="0" cellspacing="0" width="345" bgcolor="#C0C0C0" bordercolor="#FFFFFF">
<tr>
<td width="341" colspan="2">
<p align="center"><font size="6">Add Numbers</font></td>
</tr>
<tr>
<td width="103">
<b>Number 1 :</b></td>
<td width="238">
<input type="text" name="number1" size="20"></td>
</tr>
<tr>
<td width="103">
<b>Number 2 :</b></td>
<td width="238">
<input type="text" name="number2" size="20"></td>
</tr>
</center>
<tr>
<td width="341" colspan="2">
<p align="center"><input type="submit" value="Submit" name="B1">&nbsp;
<input type="reset" value="Reset" name="B2"></td>
</tr>
<tr>
<td width="341" colspan="2">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
</tr>
</table>
</div>
</form>
<p align="center">&nbsp;<a href="index.jsp">Back to Lesson 4</a></p>
<p><font size="4">&nbsp;</font></p>
<p align="center"><font color="#000080"><font size="4">For more tutorials and examples visit
</font> </font><font size="4"><a href="http://www.roseindia.net"><font color="#000080">http://www.roseindia.net</font></a></font></p>
<p align="center"><font size="4">&nbsp;</font></p>
<p align="center"><font color="#000080">Copyright © 2003 roseindia.net. All
rights reserved.</font></p>

</body>

</html>

 Code for calculator.jsp

<%@page language="java" 
import="javax.naming.*" 
import="javax.rmi.PortableRemoteObject"
import="calculator.session.*"
import ="test.session.*"
%>


<%
int number1,number2;
number1=Integer.parseInt(request.getParameter("number1"));
number2=Integer.parseInt(request.getParameter("number2"));

calculatorHome calcHome=null;

calculatorRemote calc=null;

InitialContext ic=null;

Object obj;

try{

ic=new InitialContext();
obj=ic.lookup("/ejb/CalculatorSessionBean");
calcHome=(calculatorHome)PortableRemoteObject.narrow(obj,calculatorHome.class);

}catch(Exception e){

System.out.println(e.getMessage());
}

try{
calc=calcHome.create();

}catch(Exception e){
out.println(e.getMessage());
}

%>


<html>

<head>
<title>Welcome to Jboss 3.0 tutorial</title>
</head>

<body bgcolor="#FFFFCC">

<p align="center"><font size="6" color="#800000">
Jboss 3.0 Tutorial<br>
Lesson 4</font></p>
<div align="center">
<center>
<table border="1" cellpadding="0" cellspacing="0" width="70%" bgcolor="#C0C0C0" bordercolor="#FFFFFF">
<tr>
<td width="100%" colspan="2">
<p align="center"><font size="6">Add/Multiply/Divide</font></td>
</tr>
<tr>
<td width="50%" align="right">
<b>Number 1 : =&nbsp;</b></td>
<td width="50%">
<%=number1%>
</td>
</tr>
<tr>
<td width="50%" align="right">
<b>Number 2 : =&nbsp;</b></td>
<td width="50%">
<%=number2%> 
</td>
</tr>
<tr>
<td width="50%" align="right">
<b>Add (<%=number1%>+<%=number2%>) =&nbsp;</b></td>
<td width="50%">
<%=String.valueOf(calc.add(number1,number2))%> 
</td>
</tr>

<tr>
<td width="50%"align="right">
<b>Multiply (<%=number1%>*<%=number2%>) =&nbsp;</b></td>
<td width="50%">
<%=String.valueOf(calc.multiply(number1,number2))%> 
</td>
</tr>

<tr>
<td width="50%" align="right">
<b>Divide (<%=number1%>/<%=number2%>) =&nbsp;</b></td>
<td width="50%">
<%=String.valueOf(calc.divide(number1,number2))%> 
</td>
</tr>

</center>
<tr>
<td width="100%" colspan="2">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
</tr>
</table>
</div>
<center>
<p>On this page we have called Calculator Session Bean to perform <b>Addition, Division and Multiplication.</b> <br>Our Calculator Session Bean is accessable through JNDI name "<b>/ejb/CalculatorSessionBean</b>".</center>
<p align="center">&nbsp;<a href="index.jsp">Back to Lesson 4</a></p>
<p><font size="4">&nbsp;</font></p>
<p align="center"><font color="#000080"><font size="4">For more tutorials and examples visit
</font> </font><font size="4"><a href="http://www.roseindia.net"><font color="#000080">http://www.roseindia.net</font></a></font></p>
<p align="center"><font size="4">&nbsp;</font></p>
<p align="center"><font color="#000080">Copyright © 2003 roseindia.net. All
rights reserved.</font></p>

</body>

</html>
<%
try{
calc.remove();
}catch(Exception e){
}
//calc=null;

%>


Web-Component Descriptor File

For creating example.war web.xml and jboss-web.xml files are required which explains the content of web archive. We are taking from the previous lesson (Lesson 3).

J2EE Enterprise Archive (ear) Descriptor File

For creating example.ear application.xml, file is required which explains the content of enterprise archive. Please note from this lesson we will creating example.ear for deployment and add access http://localhost:8080/example to access the application. 0

application.xml file:

<?xml version="1.0" encoding="ISO-8859-1"?>

<application>
<display-name>Example 4 </display-name>
<module>
<web>
<web-uri>example.war</web-uri>
<context-root>/example</context-root>
</web>
</module>

<module>
<ejb>example.jar</ejb>
</module>

</application>


 

Above deployment descriptor describes the content of example.ear file which contains to modules one web module example.war and one jar file example.jar.

Writing ant build file and assembling the application into enterprise archive example.ear 1

I have written ant build for compiling all source files and assembling into enterprise archive eample.ear. Here is the code of ant build file:

build.xml file:

<?xml version="1.0"?>
<!-- ==================================================== -->
<!-- Build file for our first web application -->
<!-- build.xml, Saturday, July 20, 2002 -->
<!-- Author: Deepak Kumar -->
<!-- Email : [email protected] -->
<!-- Url : http://www.roseindia.net -->
<!-- ==================================================== -->


<project name="Jboss 3.0 tutorial series" default="all" basedir=".">


<target name="init">
<property name="dirs.base" value="${basedir}"/>
<property name="classdir" value="${dirs.base}/build/src"/>
<property name="src" value="${dirs.base}/src"/>
<property name="web" value="${dirs.base}/web"/>
<property name="deploymentdescription" value="${dirs.base}/deploymentdescriptors"/>

<property name="warFile" value="example.war"/>
<property name="earFile" value="example.ear"/>
<property name="jarFile" value="example.jar"/>


<property name="earDir" value="${dirs.base}/build/ear"/>
<property name="warDir" value="${dirs.base}/build/war"/>
<property name="jarDir" value="${dirs.base}/build/jar"/>



<!-- Create Web-inf and classes directories -->
<mkdir dir="${warDir}/WEB-INF"/>
<mkdir dir="${warDir}/WEB-INF/classes"/>

<!-- Create Meta-inf and classes directories -->
<mkdir dir="${earDir}/META-INF"/>
<mkdir dir="${jarDir}/META-INF"/>


</target>

<!-- Main target -->
<target name="all" depends="init,build,buildWar,buildJar,buildEar"/>


<!-- Compile Java Files and store in /build/src directory -->
<target name="build" >
<javac srcdir="${src}" destdir="${classdir}" debug="true" includes="**/*.java" />
</target>

<!-- Create the web archive File -->
<target name="buildWar" depends="init">
<copy todir="${warDir}/WEB-INF/classes">
<fileset dir="${classdir}" includes="**/*.class" /> 
</copy>

<copy todir="${warDir}/WEB-INF">
<fileset dir="${deploymentdescription}/web/" includes="web.xml,jboss-web.xml" /> 
</copy>

<copy todir="${warDir}">
<fileset dir="${web}" includes="**/*.*" /> 
</copy>

<!-- Create war file and place in ear directory -->
<jar jarfile="${earDir}/${warFile}" basedir="${warDir}" />


</target>


<!-- Create the jar File -->
<target name="buildJar" depends="init">
<copy todir="${jarDir}">
<fileset dir="${classdir}" includes="**/*.class" /> 
</copy>

<copy todir="${jarDir}/META-INF">
<fileset dir="${deploymentdescription}/jar/" includes="ejb-jar.xml,jboss.xml" /> 
</copy>

<!-- Create jar file and place in ear directory -->
<jar jarfile="${earDir}/${jarFile}" basedir="${jarDir}" />


</target>


<!-- Create the ear File -->
<target name="buildEar" depends="init">
<copy todir="${earDir}/META-INF">
<fileset dir="${deploymentdescription}/ear" includes="application.xml" /> 
</copy>

<!-- Create ear file and place in ear directory -->
<jar jarfile="../${earFile}" basedir="${earDir}" />
</target>

</project>


 

Above file does every thing for you and creates example.ear file in the above example4 directory. To assemble the application simple run the ant build utity. 2

To deploy the application copy the file into the deploy (JBOSS_HOME/server/default/deploy) directory of JBoss 3.0. 

To test the application type http://localhost:8080/example in the browser. Which displays the following page:

3

Choose Lesson 4 and following page is displayed:

Click Add/multiply/Divide Function link, this will display the a form to accept two numbers from the user: 4

Enter the number and press "Submit" button and the calculator.jsp page will display the calculated values for you:

5

Download the code of all lessons.

In the next lesson I will show you how write Statefull Session Bean.

  6