Download Lab 5-3 - KSU Web Home

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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 click ing File ->
New Project in the menu bar at the top of the window.
3. In the New Project Wizard, choose Enterprise 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 step 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 step 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 step 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
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 0.0;
}
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 sub-node.
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. Click Finish to complete the wizard.
25.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=UTF8");
PrintWriter out = response.getWriter();
try {
InitialContext ic = new InitialContext();
calc = (CalculatorRemote)
ic.lookup("lab.calculator.sessionbeans.CalculatorRemote
");
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\" name
=\"result\" value=\"" + value + "\" />");
out.println("</form>");
} catch (Exception ex) {
System.out.println("Error:"+
ex.getMessage());
}finally {
out.close();
}
}
Also, add this to the servlet class:
private CalculatorRemote calc = null;
Finally, add this to the import lines:
import lab.calculator.sessionbeans.*;
import javax.naming.*;
26.Save the project
27.Run the project by clicking the Run icon on the tool bar.
28.Screenshot:
Lab 2: Stateful Session Bean Average Tracker
This lab will demonstrate the use of stateful session beans by creating a small
enterprise application that allows the user to enter a series of numbers through a servlet.
The stateful session bean will keep track of all numbers that a particular user has entered
and display the total and average of all numbers entered.
1. Start NetBeans by double-clicking the NetBeans icon.
2. Choose New Project from the File menu.
3. In the New Project Wizard, choose Enterprise 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 “SSBLab-ejb” in the Project pane on the left
7. Right-click on the Enterprise Beans node and choose “New → Session Bean”
from the context menu
8. In the New Session Bean wizard, name the new session bean Tracker. Enter
lab.tracker.sessionbeans as the package name. Choose Statelful as the session
type and check the Remote interface box. Deselect the Local interface box and
click Finish.
9. You will notice that the TrackerBean.java and TrackerRemote.java files have been
created in the project under the “Source Packages” node and the
lab.tracker.sessionbeans subnode. The TrackerRemote.java is the remote interface
and the TrackerBean.java file is the session bean implementation class.
10.Open the TrackerBean.java file in the editor window and add the following data
members to the class:
private double total = 0;
private int count = 0;
11.Add the (mappedName="TrackerBean") parameter to the @Stateful annotation on
the session bean. This mappedName parameter defines the name we will use to
look up the session bean.
12.Expanding the Enterprise Beans node 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.
13.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 “value” of double type. Click OK to create the business method.
14.Expanding the Business Methods node under the TrackerBean reveals the newly
created business method. The method has also been added to the Tracker session
bean implementation in TrackerBean.java and the remote interface defined in
TrackerRemote.java.
15.Repeat step 12 to create a business method named “average” with no parameters.
The method should return a double type.
16.Repeat step 12 to create a business method named “getCount” with no parameters.
The method should return an int type.
17.Repeat step 12 to create a business method named “getTotal” with no parameters.
The method should retun a double type.
18.Now that the business methods exist in the session bean implementation, add the
following code to the add() business method in TrackerBean.java in place of the
generated return statement so that the method looks like:
public double add(double value) {
total += value;
count++;
return total;
}
19.Add the following code to the average() business method in TrackerBean.java in
place of the generated return statement so that the method looks like:
public double average() {
return total / count;
}
20.Add the following code to the getCount() business method in TrackerBean.java in
place of the generated return statement so that the method looks like:
public int getCount() {
return count;
}
21.Add the following code to the getTotal() business method in TrackerBean.java in
place of the generated return statement so that the method looks like:
public double getTotal() {
return total;
}
After altering all of the pre-generated return statements the TrackerBean.java should
look like this:
@Stateful(mappedName="TrackerBean")
public class TrackerBean implements TrackerRemote {
private double total = 0;
private int count = 0;
public double add(double value) {
total += value;
count++;
return total;
}
public double average() {
return total / count;
}
public int getCount() {
return count;
}
public double getTotal() {
return total;
}
}
22.Now that the session bean has been created, expand the SSBLab-war node in the
project pane on the left, then expand the Web Pages sub-node.
23.Double-click index.jsp to open it in the editor pane.
24.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>Tracker Landing Page</title>
</head>
<body>
<h2>Tracker</h2>
<form method="post" action="Tracker">
<input type="text" name="value" />
<input type="submit" value="Add" />
</form>
</body>
</html>
25.Right-click on the SSBLab-war node and choose New → Servlet from the context
menu to open the New Servlet wizard.
26.In the New Servlet wizard, name the class Tracker and specify lab.tracker.servlets
as the package name. Click Finish to complete the wizard.
27.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=UTF8");
PrintWriter out = response.getWriter();
try {
InitialContext ctx = new InitialContext();
TrackerRemote tracker = null;
if(request.getSession().getAttribute("tracker"
) == null) {
tracker = (TrackerRemote)
ctx.lookup("TrackerBean");
request.getSession().setAttribute("tracker",
tracker);
}
else {
tracker = (TrackerRemote)
request.getSession().getAttribute("tracker");
}
double total = 0;
double average = 0;
int count = 0;
if(request.getParameter("value") !=
null) {
total =
tracker.add(Double.parseDouble(request.getParameter("va
lue")));
}
else {
total = tracker.getTotal();
}
if(tracker.getCount() != 0) {
average = tracker.average();
count = tracker.getCount();
}
out.println("Count: " + count + "<br
/>");
out.println("Total: " + total + "<br
/>");
out.println("Average: " + average +
"<br />");
RequestDispatcher rd =
getServletContext().getRequestDispatcher("/index.jsp");
rd.include(request, response);
}
catch(NamingException e) {
e.printStackTrace();
}
finally {
out.close();
}
}
28.Save the project
29.Run the project by clicking the Run icon on the tool bar.
30.
Add those three import statement:
import lab.tracker.sessionbeans.*;
31.
32.
import javax.naming.*;
import javax.servlet.*;