Deploying and testing Stateless Session Bean

Deploying and testing Stateless Session Bean

Deploying and testing Stateless Session Bean

Deploying and testing Stateless Session Bean

      

In this EJB tutorial we will build, deploy and test the Stateles Session Bean developed in the last section. We will use ant build tool to build ear file. We will deploy our application using WebLogic console.

Building ear file using ant build tool

I am assuming that you have ant build tool installed on your development environment. Download the source code from here and extract in your favorite directory.  Open command prompt and navigate to the directory <extracted directory>/sessionbean/code and type ant there. Ant build tool will compile, package and create example.ear in the  <extracted directory>/sessionbean directory. Here is the code of our ant build file:.

<?xml version="1.0"?>


<project name="WebLogic Tutorials" default="all" basedir=".">


<target name="init">
<!-- Define -->
<property name="dirs.base" value="${basedir}"/>
<property name="classdir" value="${dirs.base}/build/classes"/>
<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"/>


<!-- classpath for Project -->
<path id="library.classpath">
<pathelement path ="libext/servlet-api.jar"/>
<pathelement path ="libext/j2ee.jar"/>
<pathelement path ="libext/jdbc2_0-stdext.jar"/>
<pathelement path ="${classpath}"/>
</path>


<!-- 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"/>
<mkdir dir="${classdir}"/>


</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" >
<classpath refid="library.classpath"/>
</javac>
</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" /> 
</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,weblogic-cmp-rdbms-jar.xml,weblogic-ejb-jar.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>

Deploying the Application on Web Logic 6.0 Server

Run the WebLogic default server: 

To deploy the application we will use WebLogic console. Type http://localhost:7001/console and enter user name and password to open the WebLogic console.

Navigate to "Applications" and then click on "Install a new Application", following screen will be displayed:

Browse "example.ear" and then click on the Upload button. This will upload the file to the WebLogic server directory and then install the application.

Testing the application
To test the application open browser and type http://localhost:7001/example/index.jsp, following page should be displayed:

Click on the link "Call Session Test Servlet", it should show you the following output:

check the WebLogic server dos console, it should be displaying the message "Hello! I am Session Bean" as show below in the screen shot:

In this lesson we learnt how to deploy Session Bean and test on Web Logic Server.