Syntax
JSP pages use several delimiters for scripting functions. The most basic is <% ... %>, which encloses a JSP scriptlet. A scriptlet is a fragment of Java code that is run when the user requests the page. Other common delimiters include <%= ... %> for expressions, where the value of the expression is placed into the page delivered to the user, and directives, denoted with <%@ ... %>.
Java code is not required to be complete or self-contained within its scriptlet element block, but can straddle markup content providing the page as a whole is syntactically correct. For example, any Java if/for/while blocks opened in one scriptlet element must be correctly closed in a later element for the page to successfully compile. Markup which falls inside a split block of code is subject to that code, so markup inside an if block will only appear in the output when the if condition evaluates to true; likewise, markup inside a loop construct may appear multiple times in the output depending upon how many times the loop body runs.
The following would be a valid for loop in a JSP page:
Counting to three:
<% for (int i=1; i<4; i++) { %>This number is <%= i %>.
<% } %>Done counting.
The output displayed in the user's web browser would be:
- Counting to three:
- This number is 1.
- This number is 2.
- This number is 3.
- Done counting.
For more examples, see Java Programming/JSP at Wikibooks.
Read more about this topic: JavaServer Pages