Magazine
 
EJB 3.0

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.jsp is given below.

<%@ page contentType=”text/html;
charset=UTF-8" %>
<%@ page
import=”com.javajazzup.examples.ejb3.stateless.*,
javax.naming.*”%>
<%!
private CalculatorRemote calculator = null;
float result=0;
public void jspInit() {
try {
InitialContext ic = new
InitialContext();
calculator = (CalculatorRemote)
ic.lookup(“example/CalculatorBean/remote”);
System.out.println(“Loaded Calculator
Bean”);
} catch (Exception ex) {
System.out.println(“Error:”+
ex.getMessage());
}
}
public void jspDestroy() {
calculator = null;
}
%>
<%
try {
String s1 =
request.getParameter(“num1”);
String s2 = request.getParameter(“num2”);
String s3 =
request.getParameter(“group1”);
if ( s1 != null && s2 != null ) {
Float num1 = new Float(s1);

 

 
Float num2 = new Float(s2);
if(s3.equals(“add”))
result=calculator.add(num1.floatValue(),
num2.floatValue());
else if(s3.equals(“sub”))
result=calculator.subtract(num1.floatValue(),
num2.floatValue());
else if(s3.equals(“multi”))
result=calculator.multiply(num1.floatValue(),
num2.floatValue());
else
result=calculator.division(num1.floatValue(),
num2.floatValue());
%>
<p>
<b>The result is:</b> <%= result %>
<p>
<%
}
}// end of try
catch (Exception e) {
e.printStackTrace ();
}
%>

The source code for the “index.jsp” is given below that will actual call the client-design form.

<%@page language=”java” %>
<html>
<head>
<title>Ejb3 Stateless Tutorial</title>
</head>
<body bgcolor=”#FFFFCC”>
<p align=”center”><font size=”6"
color=”#800000"><b>Welcome to <br>
Ejb3-Jboss 4.2.0 Tutorial</b></font>
Click <a href=”ejb3/form.jsp”>Calculator
Example</a> to execute Calculator<br></p>
</body>
</html>

3. Deploy calculator application on the Application 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.

Jan  2008 | Java Jazz Up | 19
 
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