6.6 Practice Labs
6.6.1 Lab 1: Stateless Session Bean Calculator
This lab will demonstrate how to create a simple enterprise application with a web interface. The application is a calculator that will take two numbers and perform mathematical operations on the numbers. Unlike a real calculator which will keep a running total for successive operations, this calculator will simply perform one of four basic arithmetic operations on two numbers and yield a result.
1.
Start NetBeans by double-clicking the NetBeans icon.
2.
After NetBeans finishes loading, start the New Project Wizard by clicking File
→ New Project in the menu bar at the top of the window.
3.
In the New Project Wizard, choose Java EE from the category pane on the left and then choose Enterprise Application from the Projects pane on the right and click the Next button.
4.
Choose a name for the project and click the Next button.
5.
Accept the default values on the final step and click the Finish button.
6.
Expand the node labeled SBLab-ejb in the Project pane on the left.
7.
Right-click on the Enterprise Beans node under SBLab-ejb and choose New
→ Session Bean” from the context menu.
8.
In the New Session Bean wizard, name the new session bean Calculator. Enter lab.calculator.sessionbeans as the package name. Choose Stateless as the session type and check the Remote interface box. Deselect the Local interface box and click Finish.
9.
You will notice that the CalculatorBean.java and CalculatorRemote.java files have been created in the project under the package specified in step 8. To view them, expand the Source Packages node under SBLab-ejb and then expand the
package (lab.calculator.sessionbeans in this example). The
CalculatorRemote.java is the remote interface and the CalculatorBean.java file is the session bean implementation class.
10.
Expanding the Enterprise Beans node Under SBLab-ejb in the project pane reveals the newly created session bean. Right-click the session bean and choose Add → Business Method from the menu to open the Business Method wizard.
11.
In the Business Method wizard, set the name of the method to “add” and the return type to “double.” Click the Add button to add a parameter to the method named addend1 and set the type to double. Click the Add button again to add a parameter to the method named addend2 and set the type to double. Click OK to create the business method.
12.
Expanding the Business Methods node under the CalculatorBean reveals the newly created business method. The method has also been added to the
Calculator session bean implementation in CalculatorBean.java and the remote interface defined in CalculatorRemote.java in the lab.calculator.sessionbeans package.
13.
Repeat steps 10-11 to create a business method named subtract and two parameters of the double type named minuend and subtrahend. The method should return a double type.
14.
Repeat steps 10-11 to create a business method named multiply and two parameters of the double type named factor1 and factor2. The method should
return a double type.
15.
Repeat steps 10-11 to create a business method named divide and two parameters of the double type named dividend and divisor. The method should return a double type.
16.
Now that the business methods exist in the session bean, we have to make the methods do something useful. CalculatorBean.java can be found by expanding the lab.calculator.sessionbeans package under the Source Packages subnode of the SBLab-ejb node in the project. Double-click the file to open it in the editor. Add the following code to the add() business method in
CalculatorBean.java in place of the generated return statement so that the method looks like: public double add(double addend1, double addend2) { return addend1 + addend2;
}
17.
Add the following code to the subtract() business method in
CalculatorBean.java in place of the generated return statement so that the method looks like: public double subtract(double minuend, double subtrahend) { return minuend - subtrahend;
}
18.
Add the following code to the multiply() business method in
CalculatorBean.java in place of the generated return statement so that the method looks like: public double multiply(double factor1, double factor2) { return factor1 * factor2;
}
19.
Add the following code to the multiply() business method in
CalculatorBean.java in place of the generated return statement so that the method looks like: public double divide(double dividend, double divisor)
{ return dividend / divisor;
}
When you are finished, the code in CalculatorBean.java should look like this: package lab.calculator.sessionbeans;
import javax.ejb.Stateless;
@Stateless
public class CalculatorBean implements
CalculatorRemote { public double add(double addend1, double addend2)
{
return addend1 + addend2;
} public double subtract(double minuend, double subtrahend) {
return minuend – subtrahend;
} public double multiply(double factor1, double factor2) {
return factor1 * factor2;
} public double divide(double dividend, double divisor) {
return dividend / divisor;
}
}
20.
Now that the session bean has been created, expand the SBLab-war node in the project pane on the left, then expand the Web Pages subnode.
21.
Double-click index.jsp to open it in the editor pane.
22.
Edit index.jsp so that it contains the following:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Calculator Landing Page</title>
</head>
<body>
<h2>Calculator</h2>
<a href="Calculator">Click here to use the
Calculator</a>
</body>
</html>
23.
Right-click on the SBLab-war node and choose New → Servlet from the context menu to open the New Servlet wizard.
24.
In the New Servlet wizard, name the class Calculator and specify lab.calculator.servlets as the package name.
25.
Click Finish to complete the wizard.
26 . When the New Servlet wizard is complete, the new servlet will open in the editor pane. Add the following code to the processRequest() method so that it looks like:
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try { double n1 = 0.0; if(request.getParameter("n1") != null) { n1 =
Double.parseDouble(request.getParameter("n1"));
} double n2 = 0.0; if(request.getParameter("n2") != null) { n2 =
Double.parseDouble(request.getParameter("n2"));
} out.println("<form method=\"post\" action=\"Calculator\">");
out.println("<input type=\"text\" name=\"n1\" value=\"" + n1 + "\"/>"); out.println("<select name=\"op\">"); out.println("<option value=\"add\">+</option>"); out.println("<option value=\"subtract\">-</option>"); out.println("<option value=\"multiply\">*</option>"); out.println("<option value=\"divide\">/</option>"); out.println("</select>"); out.println("<input type=\"text\"
name=\"n2\" value=\"" + n2 + "\"/>"); out.println("<input type=\"submit\" value=\"=\" />");
String operation = request.getParameter("op");
String value = ""; if(operation != null && operation.equals("add")) { value += calc.add(n1, n2);
} if(operation != null && operation.equals("subtract")) {
} value += calc.subtract(n1, n2); if(operation != null && operation.equals("multiply")) { value += calc.multiply(n1, n2);
} if(operation != null && operation.equals("divide")) { value += calc.divide(n1, n2);
}
out.println("<input type=\"text\" value=\""
+ value + "\" />"); out.println("</form>");
} finally {
out.close();
}
}
26.
Save the project
27.
Run the project by clicking the Run icon on the tool bar.