It then puts the numbers, the operation, and the answer in the request
object as attributes, and finally forwards back to index.jsp, which as we??™ve seen already will
display the result.
The Workhorse:MathDelegate.java
The MathDelegate class, which MathServlet calls on, is our next stop, and it??™s very simplistic,
even more so than the servlet. For those of you who prefer a more visual approach to learning,
Figure 2-6 shows the UML class diagram for the MathDelegate class.
Figure 2-6. UML diagram of the MathDelegate class
In Listing 2-5, you can see the code for the class in its entirety.
Listing 2-5. The MathDelegate Class
package app;
/**
* A class to perform basic mathematical operations on two numbers.
*/
public class MathDelegate {
CHAPTER 2 n GETTING TO KNOW DWR 58
/**
* Add two numbers.
*
* @param a The first number.
* @param b The second number.
* @return The result of the operation.
*/
public int add(int a, int b) {
return a + b;
} // End add().
/**
* Subtract two numbers.
*
* @param a The first number.
* @param b The second number.
* @return The result of the operation.
*/
public int subtract(int a, int b) {
return a - b;
} // End subtract().
Pages:
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147