Java Reference
In-Depth Information
<% code %>
<%= expression %>
<%! code %>
<%@ directive %>
Let's look at them one at a time.
19.3.1
The code that appears between the <% and %> delimiters is called a scriptlet . By
the way, we really hate the term “scriptlet.” It seems to imply (falsely) a com-
pleteness that isn't there. It is too parallel to the term “applet,” which is a
complete Java program that runs inside a browser. A scriptlet isn't necessarily
a complete anything. It's a snippet of code that gets dropped inside the code
of the servlet generated from the JSP source.
Recall that servlets may have a doPost() and a doGet() methods, which
we collapsed in our example by having them both call the doBoth() method.
Same sort of thing is happening here with the JSP, and the doBoth() ends up
doing all the output of the HTML. Any snippets of Java code from within the
<% and %> delimiters get dropped right in place between those output calls,
becoming just a part of a method.
It can be useful to keep this in mind when writing JSP. It helps you answer
the questions of scope—who has access to what, where are variables getting
declared and how long will they be around? (Can you answer that last question?
Since any variable declared inside the <% and %> will be in the JSP equivalent
of our doBoth() method, then that variable will only be around for the dura-
tion of that one call to the doBoth() , which is the result of one GET (or POST )
from the browser.)
The source code snippets can be just pieces of Java, so long as it makes a
complete construct when all is converted. For example, we can write:
Scriptlet
<% if (acct != null) { // acct.getParent() %>
<a href="BudgetPro?func=back">
<img src="/back.gif">
</a>
<% } else { %>
<img src="/back.gif">
<% } %>
Search WWH ::




Custom Search