InfoCenter Home >
4: Developing applications >
4.2: Building Web applications >
4.2.2: Developing JSP files >
4.2.2.3: Overview of JSP file content >
4.2.2.3.3: JSP syntax: Inline Java code (scriptlets)

4.2.2.3.3: JSP syntax: Inline Java code (scriptlets)

You can embed any valid Java language code inline between the <% and %> tags. Such embedded code is called a scriptlet. If you do not specify the method directive, the generated code becomes the body of the service method.

Be sure to use the braces characters, { }, to enclose if, while, and for statements even if the scope contains a single statement. You can enclose the entire statement with a single scriptlet tag. However, if you use multiple scriptlet tags with the statement, be sure to place the opening brace character, {, in the same statement as the if, while, or for keyword. The following examples illustrate these points. The first example is the easiest.

<%
for (int i = 0; i < 1; i++)
{
out.println("<P>This is written when " + i + " is < 1</P>");
}
%>
...
<% for (int i = 0; i < 1; i++) {                                   %>
<%    out.println("<P>This is written when " + i + " is < 1</P>"); %>
<% }                                                               %>
...
<% for (int i = 0; i < 1; i++)
{                                                            %>
<%    out.println("<P>This is written when " + i + " is < 1</P>"); %>
<%    }                                                            %>

Go to previous article: JSP  syntax: Class-wide variables and methods Go to next article: JSP syntax: Java expressions

 

 
Go to previous article: JSP  syntax: Class-wide variables and methods Go to next article: JSP syntax: Java expressions