JSP Scripting Element
 
JSP Scriptlets
 
If we want to do something more complex than insert a simple expression, JSP scriptlets let us insert arbitrary code into the servlet method that will be built to generate the page.
 
Scriptlets have the following form:
<% Java Code %>
 
Scriptlets have access to the same automatically defined variables as expressions.
So, for example, if we want output to appear in the resultant page, we would use the out variable.
<%
String queryData = request.getQueryString();
out.println("Attached GET data: " + queryData);
%>
 
Note that code inside a scriptlet gets inserted exactly as written, and any static HTML (template text) before or after a scriptlet gets converted to print statements. This means that scriptlets need not contain complete Java statements, and blocks left open can affect the static HTML outside of the scriptlets.
 
For example, the following JSP fragment, containing mixed template text and scriptlets
<% if (Math.random() < 0.5) { %>
Have a <B>nice</B> day!
<% } else { %>
Have a <B>lousy</B> day!
<% } %>
 
will get converted to something like:
if (Math.random() < 0.5)
{
out.println("Have a <B>nice</B> day!");
} else {
out.println("Have a <B>lousy</B> day!");
}
 
If you want to use the characters "%>" inside a scriptlet, enter "%\>" instead.
 
Finally, note that the XML equivalent of <% Code %> is
<jsp:scriptlet>
Code
</jsp:scriptlet>
 
Remember that XML elements, unlike HTML ones, are case sensitive. So be sure to use lowercase.