Magazine
 
EJB 3.0

Note that, the @Remote annotation decorating the interface definition. This lets the container know that remote clients will access CalculatorBean.

(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.

package
com.javajazzup.examples.ejb3.stateless;
import java.math.*;
import javax.ejb.Stateless;
import javax.ejb.Remote;
@Stateless(name=”CalculatorBean”)
@Remote(CalculatorRemote.class)
public class CalculatorBean implements
CalculatorRemote{
public float add(float x, float y){
return x + y;
}
public float subtract(float x, float y){
return x - y;
}
public float multiply(float x, float y){
return x * y;
}
public float division(float x, float y){
return x / y;
}
}

Note that, the @Stateless annotation decorating the enterprise bean class. This lets the container know that CalculatorBean is a stateless session bean.

2. Creating a Web Client

The web client is divided into two pages. First is “form.jsp” where a request form is sent to the client; second is “WebClient.jsp” which is called from the “form.jsp” page.

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.

<html>
<head>
<title>Calculator</title>
</head>
<body bgcolor=”pink”>
<h1>Calculator</h1>
<hr>
<form action=”WebClient.jsp”
method=”POST”>
<p>Enter first value:
<input type=”text” name=”num1"
size=”25"></p>
<br>
<p>Enter second value:
<input type=”text” name=”num2"
size=”25"></p>
<br>
<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>
<p>
<input type=”submit”
value=”Submit”>
<input type=”reset”
value=”Reset”></p>
</form>
</body>
</html>

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”);

Jan 2008 | Java Jazz Up | 18
 
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 , 81 , 82 ,

83, 84 , 85 , 86, 87 , 88, 89 , 90 , 91 , 92 , 93 , 94 , 95 , 96 , 97 , 98 , 99 , 100 , 101 , 102 , 103, 104 , 105 ,

106, 107,

Download PDF