In the previous tutorial, we used the "out" variable in a scriptlet to generate HTML output. For more complex HTML, if we still use the "out" variable, it will lose many advantages of JSP programming. In fact, we can easily implement the combination of Scriptlets and HTML.
Suppose you want to generate a table in HTML. Creating a table is a common operation. In practice, you may want to generate a table from an SQL table or from the row of a file. To make the example as simple as possible, we generate a table that contains numbers ranging from 1 to N. Although the following examples are not very useful, you can learn some skills from them. The following is the JSP program snippet:
<Table border = 2>
<%
For (int I = 0; I <n; I ++ ){
%>
<TR>
<TD> Number </TD>
<TD> <% = I + 1%> </TD>
</TR>
<%
}
%>
</TABLE>
Before coding, you should first define an integer (int) variable "n". With this variable, We need to output the table of "n" rows. From the code above, we can notice that the %> and <% characters appear in the "for" loop. It turns out that this is to cause you to return to HTML and then to scriptlet, in fact, this is the combination of Scriptlets and HTML.
The above code is actually very simple: you can write HTML when you exit scriptlet, and then return to scriptlet. Any loop control expression, such as the "while" or "for" loop and the "if" statement can control HTML. If HTML is in a loop, it will be executed once in every loop.