Tải bản đầy đủ (.pdf) (20 trang)

The JSP Files (Part 3) - Black Light and White Rabbits

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (30.05 KB, 20 trang )

The JSP Files (part 3): Black Light And White
Rabbits
By Vikram Vaswani and Harish Kamath
This article copyright Melonfire 2000−2002. All rights reserved.
Table of Contents
Counting Down....................................................................................................................................................1
Doing More With Loops.....................................................................................................................................3
For−gone Conclusion..........................................................................................................................................5
The Sound Of Breaking Loops..........................................................................................................................8
Paying The Piper.................................................................................................................................................9
Screaming Out Loud.........................................................................................................................................11
You Say Seven, I Say 7.....................................................................................................................................14
A Positive Response..........................................................................................................................................17
The JSP Files (part 3): Black Light And White Rabbits
i
Counting Down
Last time out, you learned a little bit about the various conditional statements and operators available in JSP.
This week, we'll expand on those basics by teaching you a little bit about the different types of loops available
in JSP, discuss a few more String object methods, and take a quick tour of the new Response object.
First up, loops.
As you may already know, a "loop" is a programming construct that allows you to execute a set of statements
over and over again, until a pre−defined condition is met.
The most basic loop available in JSP is the "while" loop, and it looks like this:
while (condition)
{
do this!
}
Or, to make the concept clearer,
while (temperature is below freezing)
{
wear a sweater


}
The "condition" here is a standard conditional expression, which evaluates to either true or false. So, were we
to write the above example in JSP, it would look like this:
while (temp <= 0)
{
sweater = true;
}
Here's an example:
<html>
<head>
</head>
<body>
<%!
int countdown=30;
Counting Down 1
%>
<%
while (countdown > 0)
{
out.println(countdown + " ");
countdown−−;
}
out.println("<b>Kaboom!</b>");
%>
</body>
</html>
Here, the variable "countdown" is initialized to 30, and a "while" loop is used to decrement the value of the
variable until it reaches 0. Once the value of the variable is 0, the conditional expression evaluates as false,
and the lines following the loop are executed.
Here's the output:

30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10
9 8 7 6 5 4
3 2 1 Kaboom!
The JSP Files (part 3): Black Light And White Rabbits
Counting Down 2
Doing More With Loops
There's one caveat with the "while" loop. If the conditional expression evaluates as false the first time, the
code within the curly braces will never be executed. If this is not what you want, take a look at the "do−while"
loop, which comes in handy in situations where you need to execute a set of statements *at least* once.
Here's what it looks like:
do
{
do this!
} while (condition)
For example, the following lines of code would generate no output whatsoever, since the conditional
expression in the "while" loop would always evaluate as false.
int bingo = 366;
while (bingo == 699)
{
out.println ("Bingo!");
break;
}
%>
However, the construction of the "do−while" loop is such that the statements within the loop are executed
first, and the condition to be tested is checked after. Using a "do−while" loop implies that the code within the
curly braces will be executed at least once − regardless of whether or not the conditional expression evaluates
as true.
Doing More With Loops 3
int bingo = 366;
do

{
out.println ("Bingo!");
break;
} while (bingo == 699);
%>
Try it yourself and see the difference.
The JSP Files (part 3): Black Light And White Rabbits
Doing More With Loops 4
For−gone Conclusion
Both the "while" and "do−while" loops continue to iterate for so long as the specified conditional expression
remains true. But there often arises a need to execute a certain set of statements a specific number of times −
for example, printing a series of thirteen sequential numbers, or repeating a particular set of <TD> cells five
times. In such cases, clever programmers reach for the "for" loop...
The "for" loop typically looks like this:
for (initial value of counter; condition; update counter)
{
do this!
}
Looks like gibberish? Well, hang in there a minute...the "counter" here is a JSP variable that is initialized to a
numeric value, and keeps track of the number of times the loop is executed. Before each execution of the loop,
the "condition" is tested − if it evaluates to true, the loop will execute once more and the counter will be
appropriately incremented; if it evaluates to false, the loop will be broken and the lines following it will be
executed instead.
And here's a simple example that demonstrates how this loop can be used:
<html>
<head>
<basefont face="Arial">
</head>
<body>
<center>Turning The Tables, JSP−Style!</center>

<br>
<%!
// define the number
int number = 7;
int x;
%>
<%
// use a for loop to calculate tables for that number
For−gone Conclusion 5
for (x=1; x<=15; x++)
{
out.println(number + " X " + x + " = " + (number*x) + "<br>");
}
%>
</body>
</html>
And here's the output:
Turning The Tables, JSP−Style!
7 X 1 = 7
7 X 2 = 14
7 X 3 = 21
7 X 4 = 28
7 X 5 = 35
7 X 6 = 42
7 X 7 = 49
7 X 8 = 56
7 X 9 = 63
7 X 10 = 70
7 X 11 = 77
7 X 12 = 84

7 X 13 = 91
7 X 14 = 98
7 X 15 = 105
Let's dissect this a little bit:
Right up front, a variable is defined, containing the number to be used for the multiplication table; we've used
7 here − you might prefer to use another number.
Next, a "for" loop has been constructed, with "x" as the counter variable. If you take a look at the first line of
the loop, you'll see that "x" has been initialized to 1, and is set to run no more than 15 times.
The JSP Files (part 3): Black Light And White Rabbits
For−gone Conclusion 6

×