As you may know from previous experience with web projects, all information that goes to or from a browser must be in the form of a String (text). This means that if a user enters "15" on a form, the browser and servlet do not know that this is really a numeric value that you can use in mathematical operations. AppComposer provides two actors that enable you to turn Strings into numbers, or turn numbers into Strings that a browser can display.
The MathProcess capsule needs both of these actors. NumberParser will convert input from the forms into values that the capsule can use mathematically, and NumberFormatter will take the results of the calculation and send them back in a format the browser can print.
Since both sine and reciprocal operations often result in numbers with long fractional values, you should set a reasonably wide range of decimal places for the display of results, as you did in this procedure.
Since the page constructs a table of the output from a given series of numbers, you need some variables that will track your place in the number series, and hold each value that the servlet needs to process. To track these, you need three variables. One to hold the current value, one to hold the next value, and one to track the current place in the sequence. Java defines different kinds of data types that you can use for variables. The first two variables need to be double types, which can have decimals, since many of the results of the calculations may have decimal values. The variable holding the current step is an integer type, since you would only count each step in whole numbers (1, 2, 3 ...).
Because this servlet may be called by different users, or may be called to execute its functions several times, you should ensure that the variables are initialized to an appropriate value before any processing occurs. This prevents any values lingering from a previous execution from interfering with the current one. Since you want to initialize the variables each time you get form input from a browser, a good place to add the initialization is to the ServletPost action group. That way, every time the servlet receives a POST request, nextValue and currentStep get reset appropriately.
This behavior looks for the initialValue parameter from the form, and uses NumberParser to turn it into a numeric value from a String. It uses nextValue to hold this number.
nextValue
and currentStep
values need to be
initialized before the JSP document starts being output. Click the move
down
button on the toolbar twice. By moving sendDocument
below the
two initialize
behaviors you just created, you make it execute
after the other behaviors. ServletPost now does considerably more than just send a document in response to a POST request. It is always good practice to give behaviors names that reflect what they do.
Now that you have included the variables and actors you need for numeric processing, you can start to define the behaviors that respond to the JSP tags in mathtable.jsp. The next topic introduces the different types of tags that AppComposer defines, and takes you through creating the behaviors that process the requests from the JSP page.