link to table of contentsgo to previous topicgo to next topiclink to glossaryindex of terms

 

Creating Numeric Processing

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.

To add NumberParser:

  1. Select MathTablePage in the outline.
  2. From the Palette, select Misc -> NumberParser.

To add and configure NumberFormatter:

  1. Select the MathTablePage in the outline again.
  2. From the Palette, select Misc -> NumberFormatter
  3. In the Details pane, set the MaximumFractionDigits to 5.
  4. Set the MinimumFractionDigits to 1.

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.

Defining Your Variables

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 ...).

To add the variables:

  1. Select MathProcess in the outline.
  2. Insert a double data item.
    Insert -> Data Item -> double
  3. Rename the double currentValue.
  4. Select MathProcess in the outline.
  5. Insert a double data item.
    Insert -> Data Item -> double
  6. Rename the double nextValue.
  7. Select MathProcess in the outline.
  8. Insert an int data item.
    Insert -> Data Item -> int
  9. Rename the int currentStep.

Initializing Variables

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.

To initialize nextValue:

  1. Select ServletPost from the outline.
  2. Insert a new action behavior from the Palette.
    Behaviors -> Action
  3. Name the behavior initialize nextValue.
  4. Edit initialize nextValue to match the following settings:

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.

To initialize currentStep:

  1. Select ServletPost from the outline.
  2. Insert a new action behavior from the Palette.
    Behaviors -> Action
  3. Name the behavior initialize currentStep.
  4. Edit initialize currentStep to match the following settings:

To finish editing ServletPost:

  1. Select sendDocument from the outline.
  2. The 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.
  3. Select ServletPost from the outline.
  4. Rename ServletPost to ServletPost - init and print.

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.



link to table of contentsgo to previous topicgo to next topiclink to glossaryindex of terms
      © 2003 DigiSlice Corporation