add(a, b);
request.setAttribute("op", "+");
} else if (op.equalsIgnoreCase("subtract")) {
answer = mathDelegate.subtract(a, b);
request.setAttribute("op", "-");
} else if (op.equalsIgnoreCase("multiply")) {
answer = mathDelegate.multiply(a, b);
request.setAttribute("op", "*");
} else if (op.equalsIgnoreCase("divide")) {
answer = mathDelegate.divide(a, b);
request.setAttribute("op", "/");
}
// Add the two numbers and the answer to request, so our JSP can display it.
request.setAttribute("a", new Integer(a));
request.setAttribute("b", new Integer(b));
request.setAttribute("answer", new Integer(answer));
// Forward to index.jsp.
RequestDispatcher dispatcher =
CHAPTER 2 n GETTING TO KNOW DWR 57
getServletContext().getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response);
} /// End doPost().
} // End class.
This is a pretty ordinary servlet really. You??™ll quickly note that this isn??™t exactly productionquality
code! All of the checks you??™d perform to avoid errors are left out, among other things,
but that is just for the sake of keeping the listing relatively short in print. In any case, you
should get the general gist of it in short order. It grabs the two numbers the user submitted,
along with the operation the user selected, and calls on the MathDelegate class to perform the
desired operation.
Pages:
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146