Notice that I??™m using a scriplet block instead of JSTL or something more elegant because
this is frankly very simple code, and I felt it was cleaner to not bring in extra dependencies and
have more to talk about. To explain the code you see quickly: when the form is submitted, it
will hit the MathServlet, which we??™ll see next. That will call the MathDelegate class, which is
what does the actual math. The servlet then puts a number of attributes into the request
object, including the two numbers (the attributes a and b) the user submitted, as well as the
mathematical symbol for the operation the user requested (the op attribute), and of course the
answer. So, if the answer attribute is found, then we grab the two numbers and the operation
from the request object. Then, we just output the result, which is the complete expression,
answer and all. Certainly not rocket science!
On the Server Side:MathServlet.java
Only one step remains, and that??™s to create the Java classes for the MathServlet and the
MathDelegate that the preceding JSP makes use of. Please save Listing 2-4 and Listing 2-5
(the latter shown in the next section) to the simpleapp/WEB-INF/src/app directory.
Pages:
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144