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

 

Defining JSP Tags

If you open the file mathtable.jsp in a text or HTML source editor, you will see the following lines at the very top:

           <%page language="java" buffer="4kb" isErrorPage="false" %>
           <%taglib uri="WEB-INF/lib/SpinTags.jar" prefix="composer" %>

Any JSP file you create that uses behaviors defined in AppComposer needs to include these lines. The first line tells the JSP compiler that the page is a Java Server Page. The second line tells the JSP compiler where to look for a definition of the composer tags in the file.

If you know XML (extensible markup language) the form of the tags will look familiar to you. You do not need to know XML to use JSP tags, but it may make it a little easier to understand how they work. At the most basic level, there are two forms a tag might take. In the first, a tag encloses a certain amount of content in the JSP file. For instance:

           <composer:conditional method="showSquare">
                <TD> X * X </TD>
           </composer:conditional>

This type of tag determines whether, or how, the content it encloses gets displayed. The slash in the end tag closes the tag, so the JSP knows to move on to the next thing.

The second possible form is a single tag with a slash at the end:

           <composer:call method="getMyParameter"/>

This type of tag usually inserts something into the JSP file. All of the content for this tag comes from the servlet, so it does not enclose anything in the JSP file, and the slash is right there at the end of the tag.

AppComposer provides several kinds of tags. Each one has slightly different behavior to enable you to do common tasks easily. Here are some of the tags:

Tag Behavior
call Calls the method in the servlet. You can use this tag to insert content into the JSP output stream.
getProperty Gets a property from the servlet capsule, or from a bean contained in the capsule. You can use this tag to insert content into the JSP output stream.
conditional Outputs content enclosed by the tag whenever the servlet evaluates the method's associated behavior to true.
repeat Repeats content enclosed by the tag as long as the servlet evaluates the method's associated behavior to true.

 

About Conditional Tags

When the JSP processes a <composer:conditional> tag, the tag calls a method in the servlet capsule to decide whether or not to output any content that the tag encloses. Generally, the servlet receives the method from the JSP and activates a corresponding behavior to determine whether any given condition, such as: a value being within a certain range, a user having entered a value onto a form, or any other condition, is true or false. If the servlet evaluates the condition positively, the JSP outputs any material enclosed by the conditional tag. This can include other JSP tags that call back to the servlet. If the servlet determines that the condition is not met, the JSP skips any material enclosed by the conditional tag.

How mathtable.jsp uses conditional tags

This JSP only needs to respond to one possible outcome of a condition. If a user has checked a certain box on an input form, the JSP needs to calculate and display results for the operation the box represents. If the box is not checked, the JSP file does not need to show that column of the table. You need to create one conditional tag in the JSP and conditional method in the servlet for each box on the form.

The file mathtable.jsp uses three conditional methods: showSquare, showReciprocal, and showSine. These methods ask the servlet whether a user has checked the corresponding boxes on the input form. The servlet will receive this information from parameters posted to it by the mathmain.html form. If a box has been checked, then mathtable.jsp needs to display the outcome of that operation for the series of values given. If the box was not checked, mathtable.jsp should skip any output related to that operation.

The names you give to the corresponding behaviors in the outline pane of the capsule do not have to match the method names from the JSP file, although it is always a good idea to give your behaviors names that indicate their functions. What you do need to ensure is that the spelling and capitalization of the activating event in the behavior's Stimulus fields corresponds to the method in the JSP file exactly.

Since the behaviors corresponding to the conditional methods in mathtable.jsp are similar to each other, you can create show square first, and then copy and edit it to create the other two.

To define showSquare:

  1. Select MathTablePage from the outline.
  2. Insert a new ReturnValue behavior from the Palette.
    Insert -> Other Behaviors -> ReturnValue
  3. Name the behavior showSquare.
  4. Edit the behavior so it has the following settings:

This behavior checks whether a user has marked the Calculate the square option on the form. The "!=" part of the Expression: line means "does not equal". It is the opposite of the "= =" sign, which means "equals". The Expression: line checks that the square parameter is not empty, which means the box was checked. When you have an HTML form with a checkbox, it may seem intuitive to think that if you check the box, the parameter sends the servlet the equivalent of a "true" value, and if the box is not checked, the servlet gets sent a "false" value. Actually, if the box is not checked, that parameter does not get sent to the servlet at all. So when you have any value from a checkbox, you know the box was checked, and you do not have to see what the actual value is.

When the JSP encounters the <composer:conditional method="showSquare"> tag, it calls the showSquare method that you defined on the capsule. This method triggers the ReturnValue behavior you just created, which then returns true or false, depending on whether or not the checkbox was checked. The JSP displays or skips the content of the tag depending on whether true or false was returned.

To define showReciprocal:

  1. Hold the CTRL key and drag showSquare's icon onto the icon for MathTablePage.
    This creates a copy of showSquare as a child of MathTablePage.
  2. Name the new behavior showReciprocal.
  3. Change the following settings so that the behavior responds properly to a showReciprocal conditional method call:

This behavior checks whether a user has marked the Calculate the reciprocal option on the form, and returns true or false.

To define showSine:

  1. Hold the CTRL key and drag showSquare's icon onto MathTablePage.
    This creates a copy of showSquare as a child of MathTablePage.
  2. Name the new behavior showSine.
  3. Change the following settings so that the behavior responds properly to a showSine conditional method call:

This behavior checks whether a user has marked the Calculate the sine option on the form.

Check the work that you have done so far. You have not defined all of the behaviors necessary for the servlet to calculate the results of these operations, but the JSP should display columns for a table that correspond to the boxes that the user checked.

To run the servlet:

  1. Make sure the debug server is enabled.
  2. Open your browser to http://localhost/Math/mathmain.html.
  3. Check some or all of the checkboxes for Square, Reciprocal and Sine.
  4. Click Build the Table.

If the servlet has been set up correctly, you should see the header row of a table with columns that correspond to the operations you chose. There aren't any results yet, even if you specified a sequence of numbers, because you have not yet defined in the servlet how to deal with those numbers.

If you do not see a similar page, check that the method you designated as the activating event for each behavior matches exactly, in spelling and capitalization, the conditional tag in the file mathtable.jsp. If they do not match exactly, the capsule listens for an activating event that never arrives, and ignores the call from the servlet. Check also that the parameter name in each Expression: line matches the parameter name in the source of mathmain.html.

About GetProperty and Call Tags

The mathtable.jsp file also contains many tags with the syntax <composer:call /> and one <composer:getProperty />. Unlike conditional method tags, call method and getProperty tags do not have a separate closing tag. This is because, instead of determining whether to output content enclosed by a conditional tag, these tags request content from the servlet, and insert that content into the JSP's output in place of the tag. For instance, if the JSP had a tag: <composer:call method="getMyParameter"/> and the value of myParameter was "My name is Bob", then when the JSP file was output, it could replace the entire call tag in the file with "My name is Bob".

The JSP file in this project uses one getProperty tag and three call tags. These correspond to the possible columns in the results table. The tags fill in the values that belong in each cell of the table. The getProperty tag requests the value that the servlet is operating on. Since the servlet processes a series of numbers, the value will change as the servlet goes through the series. Each value gets output into a cell in the table.

The other three call tags request the values that result from the mathematical operations on each number in the series. These are getSquare, getReciprocal, and getSine. Notice that these call tags are nested within conditional tags. The effect of this is straightforward: each conditional method checks to see if the user requested that operation. If not, the JSP skips to the next tag. If so, the JSP outputs the content enclosed by the tag, which is the call method request for the appropriate value.

To define the currentValue property:

The getProperty tag, <composer:getProperty property="currentValue" format="number"/>, requests the currentValue property of the capsule, and formats the value as a number. You need to create a currentValue property in the servlet capsule, bound to the currentValue variable that you created earlier.

  1. Select the MathProcess capsule in the outline.
  2. Click the Properties button to create a new property for the capsule.
  3. Name the property currentValue.
  4. Bind the property to the currentValue variable.
  5. Click OK.

Now the getProperty tag will retrieve the value of the currentValue variable. The format="number" attribute in the tag specifies that the value should be output as a number.

To define getSquare:

Defining the first method, getSquare, is straightforward.

  1. Select MathTablePage in the outline.
  2. Insert a new ReturnValue behavior from the Palette.
    Behaviors -> ReturnValue
  3. Name the behavior getSquare.
  4. Edit getSquare to have the following settings:

This behavior responds to the servlet receiving a getSquare request by looking at the number held in the currentValue variable, squaring it, and using the NumberFormatter to return the result as a string that the JSP can send out with its HTML output.

The next two behaviors, getReciprocal and getSine, are more complex. In the next topic you will learn how to use two new types of behaviors, an IfTest and a script, to process these calls from the JSP.

 



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