Making Decisions, Decisions - How It Works
(Page 6 of 11 )
You’ve already examined the HTML code quite closely, so you’ll now concentrate on the second file,whenexamplehandler.jsp. This file checks whether quantity is greater than zero using the EL inside a JSTL <when> tag:
<c:when test="${param.quantity > 0}">
If this is true, what appears before the closing <when> tag will be used for the final page, which here is simply the message: Thank you for your order!! As you see, the <c:when ></c:when> tags act like the if { } block in normal Java code.
If, however, the EL condition in the <when> tag isn’t true, then you show the message Sorry, please enter a positive quantity:
<c:otherwise>
Sorry, please enter a positive quantity
</c:otherwise>
In this case, the <c:otherwise></c:otherwise> tags behave like a Java else block. Be warned that the relationship between if...else and <c:if>...<c:otherwise> isn’t perfect. Some have likened the JSTL tags to the switch statement, which is covered in the “Using the switch Statement” section.
Notice that you could display the quantity of items ordered on this page quite easily by inserting the following EL in whenexamplehandler.jsp:
<c:when test="${param.quantity > 0}" >
Thank you for your order of ${param.quantity} items!! </c:when>
The final question in the “Trying It Out: Using the JSTL with Your Pizza Form” section at the end of Chapter 3 touched on the problem of ensuring your users submit meaningful data. In this example, for instance, there’s nothing to stop a user from entering letters or other non-numeric characters in the Quantity box. The JSTL will attempt to convert the parameter value to an appropriate type (an integer in this case), but often conversion simply isn’t possible, resulting in a screen of user-scaring error messages, as you can see in Figure 5-5.

Figure 5-5. A string has been entered in the form
It’s important to prevent such unprofessional outcomes in your programs, and you do it by the process of input validation. Validation involves anticipating places where inappropriate input is possible, handling such eventualities by checking the submitted input, and displaying a warning of your own if the input isn’t of the correct type. This could involve adding code to the page to perform this task, or you could pass the responsibility off to another component. Chapter 10 introduces Struts, a framework that, among many other things, makes the validation of user input fairly simple.
You can use an if statement to gain some simple protection. The final example in Chapter 3 checked that the title wasn’t null but not the price. And it didn’t check to see that the price wasn’t a negative number. As a partial solution, you could change the conditional logic to the following:
<c:if test="${(param.title != null) && (param.price > 0)}">
Unfortunately, it’s still possible that an error could occur should price not be a number. The handling of errors (known as exceptions in Java) that may occur is an important consideration for any programmer.
Nesting if Statements
You can model this “choice within a choice” in Java by embedding an if statement or an if...else statement inside another if or if...else statement. This is known as nesting if statements and is generally as follows (the nested statements are in bold):
if (expression1)
{
if (expression2)
{
Statement Block 1
}
}
else
{
if (expression3)
{
Statement Block 2
}
else
{
Statement Block 3
}
}
This ability to nest statements shouldn’t be entirely unexpected. Because a statement block is simply one or more lines of Java code, there’s no reason why an if statement block shouldn’t contain further if and if...else statements.
You’ll now see a code snippet to illustrate this point. You’ll return to the earlier shipping cost scenario. Here, your customer is allowed free shipping only if more than four items are purchased. If he spends more than $100 on his five or more items, then the customer gets a 5 percent discount as well. However, if he spends more than $2,000 on the five or more items, the customer gets a free television:
if ( itemQuantity > 4 )
{
System.out.println("Free Shipping for You!!");
if (itemCost > 100)
{
System.out.println("5% Discount for You!!");
if (itemCost > 2000)
{
System.out.println("Free Television for You!!");
}
}
}
Notice that you have more than one level of nesting here: You can have as many levels as you need, and you can create the same construct with JSTL <choose> tags:
<c:choose>
<c:when test="${itemQuantity > 4}" >
<c:choose>
<c:when test="${itemCost > 2000}">
You've earned a free television, plus a 5% discount and free shipping.
</c:when>
<c:when test="${itemCost > 100}">
5% discount on your purchase, in addition to no shipping charge.
</c:when>
<c:otherwise>
We will ship your order free of charge.
</c:otherwise>
</c:choose>
</c:when>
</c:choose>
Next: Understanding the Conditional Operator >>
More Java Articles
More By Apress Publishing
|
This article was excerpted from Beginning JSP 2: From Novice to Professional by Peter den Haan et. al. (Apress, 2004; ISBN: 1590593391). Check it out at your favorite bookstore. Buy this book now.
|
|