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


Creating Loops

Loops are one of the most basic and useful techniques in any programming system. As you would expect, a loop allows you to execute a certain behavior for a given number of times automatically. You can create loops that execute a certain set number of times, or that continue as long as a given condition is met, or that only stop when a certain condition is met. AppComposer provides several ways to create looping behaviors, and a specialized JSP tag to make easy use of them in web applications.

About Repeat Methods

When a JSP file encounters a <composer:repeat> tag, it calls to the servlet looking for a true response to the method or activation condition. If you have a group of behaviors that make up a loop that you want repeated in the JSP tag, the last one in the group needs to return true. If the JSP does not receive this indicator, it stops executing the tag, and moves on to the next part of the JSP file.

About Conditional Activation

Conditional activation is an extremely useful mechanism. It executes a certain behavior or behaviors only if a given condition is true. When you use conditional activation in conjunction with a repeat JSP tag, this mechanism easily creates looping behavior. For instance, in the project you are creating for this tutorial, you need to print a table of results for a series of numbers. Using a repeat tag that calls a conditionally activated behavior makes it possible to print as many rows as necessary for the series of numbers you are using, and then stop when you have displayed them all. A similar concept you may be familiar with from other programming languages is a "while" loop. While the condition in the activation expression is true, the associated behaviors repeatedly execute.

AppComposer makes creating conditional activation easy. You have already created many behaviors that execute from: Receive: <some event>. To create a conditional behavior, you change the Activate On field in the Details pane to Activate If. Once you do, a new field pops up labeled If: Use this field to enter a boolean (true/false) condition to evaluate when the behavior receives its activating event.

For the Math.zap project, you need to create a group of behaviors that prints a table that displays the results received from the chosen calculations. You need to set up a loop that prints one row at a time, starting with the current number in the series, and calculating the results for that number. The loop needs to repeat for as many numbers as are in the series, and stop after it reaches the end number in the series.

To add an action group to print a table row:

  1. Select MathTablePage from the outline.
  2. Insert an action group behavior as a child of MathTablePage.
    Behaviors -> ActionGroup
  3. Name the action group print table data.
  4. Give print table data conditional activation:
    Toggle Activate On: to Activate If:
  5. Edit print table data so that it matches the following settings:

You will add behaviors to the print table data action group that lay out and fill in the results table for the operations that the user chooses. The group also needs a behavior that sends a message of true to the JSP when it executes. This true message is what the repeat tag in the JSP uses to know that it should call the loop again. As long as the condition in the If field is met for the group, all of the behaviors will execute in a loop fashion. Once the conditional activation becomes false, the loop stops, no true message is sent, and the JSP moves on to the next part of its content.

To add set currentValue:

  1. Select print table data in the outline.
  2. Insert an action behavior as a child of print table data.
    Behaviors -> Action
  3. Name the new behavior set currentValue.
  4. Edit set currentValue to match the following settings:

This behavior updates the variable currentValue, which is what the capsule uses to calculate the given operations, to the value held in nextValue. This initializes currentValue so it is ready for calculations on the next number in the given series, and also for when the JSP getProperty tag requests the currentValue property of the capsule.

To add set nextValue script:

  1. Select print table data in the outline.
  2. Insert a new script behavior as a child of print table data.
    Behaviors -> Script
  3. Name the script set nextValue.
  4. Edit set nextValue to match the following settings:
    double startValue = NumberParser.parseDouble(capsule.getParameter("initialValue")); double endValue = NumberParser.parseDouble(capsule.getParameter("endValue")); long totalSteps = NumberParser.parseLong(capsule.getParameter("steps")); double increment = (endValue - startValue) / (totalSteps - 1); nextValue = startValue + increment * (currentStep + 1);

The script set nextValue uses a series of Java statements to:

Scripts can perform any series of Java statements. Each statement must use correct Java syntax.

To add increment currentStep:

  1. Select print table data in the outline.
  2. Add a new action behavior as a child of print table data.
    Behaviors -> Action
  3. Name the new behavior increment currentStep.
  4. Edit increment currentStep to match the following settings:

The increment currentStep behavior adds one to the value of the current step, so that the correct number in the series can be properly calculated by the servlet capsule.

To add the continue looping behavior:

  1. Select print table data in the outline.
  2. Add a new action behavior as a child of print table data.
  3. Name the new behavior continue looping.
  4. Edit continue looping to match the following settings:

The continue looping behavior sets the capsule's Test Result to true. You previously created methods that returned boolean (true/false) values for the conditional tags. An alternate way of returning a value to the JSP is to set the capsule's Test Result. If your method doesn't return a boolean, the JSP checks this test result. In this case, we used conditional activation in the ActionGroup, so if the loop should not continue, no behavior is activated, and the Test Result defaults to being false for each call from the JSP. You can either create a method that returns a boolean value, or use capsule.setTestResult(), which-ever seems easier, to respond to conditional or repeat tags.

The true test result tells the JSP to process the repeat tag again. When it does, it activates the print table data group, which checks that the If condition is still met. If it is, it executes all of its child behaviors, true is sent out again, and the whole process loops. As soon as the condition is not met (in this case, as soon as the number held in the currentStep variable equals the number of steps specified by the user,) the action group does not activate, the true test result is not set, and the JSP moves on.

You have finished creating the capsule! Try it out.

To run the project:

  1. In your browser, open to the form input page at http://localhost/Math/mathmain.html.
    Netscape 4.7 cannot process this servlet when run locally, so try it in a different browser.
  2. Select all of the options for square, reciprocal, and sine.
    Since you are testing, test whether all the behaviors work before narrowing it down for any problems.
  3. Enter 0 for your start number.
  4. Enter 5 for your end number.
  5. Enter 6 for how many numbers you want the series to include.
    You could use any real values, but try these first so that it is easy to ensure that the calculations come out correctly.
  6. Press Build the Table.

Your screen should now resemble the following:

If it does not, look at the following potential trouble areas.

Since this is a complex capsule with many interlocking behaviors, you may need to dig a little deeper than this to find any errors. If so, you need to do some debugging. The next lesson shows you the tools that AppComposer provides that allow you to test and debug your servlet capsules.


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