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:
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:
|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.|
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.
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.
boolean, and click OK. No argument is needed for this method.
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
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
boolean, and click
This behavior checks whether a user has marked the Calculate the reciprocal option on the form, and returns true or false.
boolean, and click
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.
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.
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.
The getProperty tag,
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
Now the getProperty tag will retrieve the value of the currentValue variable.
format="number" attribute in the tag specifies that
the value should be output as a number.
Defining the first method, getSquare, is straightforward.
String, and click
OK. No argument is needed for this method.
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.