JSP from simple to deep (5) -- a mix of Scriptlets and HTML

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.