< Zurück | Inhalt | Weiter >

19.3.1 Scriptlet

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:


<% if (acct != null) { // acct.getParent() %>

<a href="BudgetPro?func=back">

<img src="/back.gif">

</a>

<% } else { %>

<img src="/back.gif">

<% } %>


Notice how the if-else construct is broken up into three separate scriptlets—that is, snippets of code. Between them, in the body of the if and the else, is plain HTML. Here is what that will get translated into after the JSP conversion:


if (acct != null) { // acct.getParent() out.println("<a href=\"BudgetPro?func=back\">"); out.println("<img src=\"/back.gif\">"); out.println("</a>");

} else {

out.println("<img src=\"/back.gif\">");

}


Do you also see why we describe it as being “turned inside out”? What

was delimited becomes undelimited; what was straight HTML becomes delimited strings in output statements.

As long as we’re on the topic of conversion, let’s consider comments. There are two ways to write comments in JSP, either in HTML or in Java. In HTML we would write:


<!-- HTML comment format -->


but since we can put Java inside of delimiters, we can use Java comments, too:


<% // Java comment format %>


or even:


<% /*

* Larger comments, too.

*/

%>


If you’ve been following what we’ve been saying about translation of JSP

into Java code, you may have figured out the difference. The Java comments, when compiled, will be removed, as all comments are, from the final executable. The HTML-based comments, however, will be part of the final HTML output. This means that you’ll see the HTML comments in the HTML that reaches the browser. (Use the View Source command in your browser to see them. As HTML comments, they aren’t displayed on the page, but they are sent to the browser.) This is especially something to be aware of when writing

a loop. Remember our loop for generating the table?


<% // for each subaccount:

for (Iterator actit = acct.getAllSubs(); actit.hasNext(); ) { Account suba = (Account) actit.next();

%>


...


<!-- Next Row -->

<tr>

<td><a href="BPControl?name=<%= suba.getName() %>&func=cd">

<% } // next acct %>


We’ve put a comment just prior to the <tr> tag. What will happen is that the comment will be part of the generated HTML, and since this is a loop, the comment, just like the <tr> and other tags, will be repeated for each iteration of the loop. Now we’re not saying this is undesirable—in fact it makes the re- sultant HTML more readable. But be aware that these comments will be visible to the end user. Be careful in what you say in them. The additional transmission time required for these few extra bytes is probably imperceptible, unless your comments are large and repeated many times.