In this part of Enterprise Session Beans, you will
learn how to develop, deploy, and run a simple Java EE application named example
using stateless session bean.
The purpose of example is to performs the mathematical operations such
as Addition, Subtraction, Multiplication, and Division.
The example application consists of an enterprise bean, which performs the calculations, and two types of clients: an application client and a web client.
There are following steps that you have to follow to develop a example JEE application.
Create the enterprise bean: CalculatorBean
Create the web client: WebClient
Deploy example onto the server.
Using a browser, run the web client.
I. Creating the enterprise bean:
The enterprise bean in our example is a stateless session bean called CalculatorBean. The source code for CalculatorBean is in ?net/roseindia/ejb3/stateless? directory.
Creating CalculatorBean requires these steps:
(i) Coding the bean?s Remote business interface and Enterprise bean class.
(ii) Compiling the source code with the Ant tool.
(i) Coding the Business Interface
The business interface defines the business methods that a client can call remotely. The business methods are implemented in the enterprise bean class. The source code for the CalculatorRemote business interface is given below.
Note that, the @Remote annotation decorating the interface definition. This lets the container know that CalculatorBean will be accessed by remote clients.
II. Coding the Enterprise Bean Class
The enterprise bean class for this example is called CalculatorBean. This class implements the four business methods (add, subtract, multiply, division) that are defined in the CalculatorRemote business interface. The source code for the CalculatorBean class is given below.
Note that, the @Stateless annotation decorating the enterprise bean class. This lets the container know that CalculatorBean is a stateless session bean.
Compiling and Packaging the example Example
Now you are ready to compile the remote business interface (CalculatorRemote.java), the enterprise bean class (CalculatorBean.java) and the application client (CalculatorClient.java), then package the compiled classes into an enterprise bean JAR.
II. Creating the calculator Web Client
The web client is contained in the JSP page "WebClient.jsp". A JSP page is a text-based document that contains JSP elements, which construct dynamic content, and static template data, expressed in any text-based format such as HTML, WML, and XML.
The source code for the ?form.jsp? is given below.
<form action="WebClient.jsp" method="POST">
<p>Enter first value:
<input type="text" name="num1" size="25"></p>
<p>Enter second value:
<input type="text" name="num2" size="25"></p>
<b>Seclect your choice:</b><br>
<input type="radio" name="group1" value ="add">Addition<br>
<input type="radio" name="group1" value ="sub">Subtraction<br>
<input type="radio" name="group1" value ="multi">Multiplication<br>
<input type="radio" name="group1" value ="div">Division<br>
<input type="submit" value="Submit">
<input type="reset" value="Reset"></p>
The following statements given below in ?WebClient.jsp? are used for locating the business interface, creating an enterprise bean instance, and invoking a business method.
|InitialContext ic = new InitialContext();
CalculatorRemote calculator = (CalculatorRemote)ic.lookup("example/CalculatorBean/remote");
The classes needed by the client are declared using a JSP page directive (enclosed within the <%@ %> characters). Because locating the business interface and creating the enterprise bean are performed only once, this code appears in a JSP declaration (enclosed within the <%! %> characters) that contains the initialization method, jspInit, of the JSP page. A scriptlet (enclosed within the <% %> characters) retrieves the parameters from the request and converts it to a Float object. Finally, a JSP scriptlet invokes the enterprise bean?s business methods, and JSP expressions (enclosed within the <%= %> characters) insert the results into the stream of data returned to the client.
The full source code for the WebClient program is given below.
Note: The Application Server automatically compiles web clients that are JSP pages. If the web client were a servlet, you would have to compile it.
The source code for the ?index.jsp? is given below that will actual call the client-design form.
|<[email protected] language="java" %>
<title>Ejb3 Stateless Tutorial</title>
III. Deploy example onto the server
To deploy the created example application we are going to use Jboss 4.2.0 Application Server about which you have read in the previous section of this Javajazzup issue. So you first need to download the following tools to deploy this application.
Do the following steps to deploy the example application:
(i) Make a directory structure. You can Click here to extract the readymade directory structure according to this tutorial.(ii) Create the essential deployment descriptor .xml files.
<project name="Jboss Tutorials" default="all" basedir=".">
classpath for Project -->
<!-- Create Web-inf and classes directories -->
<!-- Main target -->
<target name="all" depends="init,build,buildWar,buildJar,buildEar"/>
<!-- Create the web archive File -->
<!-- Create the jar File -->
<!-- Create jar file and place in ear directory -->
Put this file in the base
Put this file in the Stateless\code\deploymentdescriptors\ear directory.
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
Put this file in the Stateless\code\deploymentdescriptors\web
Put all .jsp files in the Stateless\code\web directory.
Put all .java files in the Stateless\code\src directory.
(iii) Start command prompt, and go to the Stateless\code directory. Then
type the command as:
The Ant tool will deploy the example.ear file to the jboss-4.2.0.GA\server\default\deploy directory.
V. Running the example Web Client
Open the web browser and type the following URL to run the application:
Click at the given link as Calculator Example:
Give values to the textbox and choose the desire option button as Addition then clicks the Submit button to get the result.
The result is: 9.0
Posted on: June 10, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles