JavaServer Pages - Scriptlets
(Page 3 of 7 )
Scriptlets contain Java code statements. The code in the scriptlet appears in the translated JSP, but not in the output to the client. Any legal Java code statements can appear within a scriptlet. For example, to repeat the phrase "Hello, World!" ten times in the output page, you could use this scriptlet:
<%
for (int i = 0; i < 10; i++) {
%>
Hello, World!
<%
}
%>
As in this code snippet, we can freely interleave Java code and HTML and/or text data. Everything between the scriptlet markers (<% and %> ) is script code; everything outside the markers is template data, which is sent to the client as written. Notice that in the above example the Java code block does not need to begin and end within the same scriptlet element. This allows you complete freedom to mix Java code and HTML elements as needed within the page.
The above example is relatively simple. However, as your application gets more complicated and involved, youll get more and more code mixed in with the HTML and the page will tend to get complicated. In the next chapter, we will see how tag libraries can give the same rich behavior as above, but using only XML tags.
Since scriptlets can contain Java statements, the following is a legal scriptlet:
<%
Vector v = new Vector();
// more code...
%>
This looks very similar to the code snippet in the declaration section that preceded this section. This might lead you to wonder what the difference between scriptlets and declarations is, since they appear to be the same. Despite that seeming similarity, they are different in the following ways:
- Scriptlets cannot be used to define a method; only declarations can be used for that.
- Variables declared in a declaration are instance variables of the JSP page implementation class. These variables are visible to all other code statements or methods in the page.
- Variables declared in a scriptlet are local to a method in the JSP page implementation class. They are visible only within their defining code block.
Expressions
Expressions are used to output the value of a Java expression to the client. For example, this code fragment in a JSP:
The number of tokens in this statement is <%= countTokens ("The number of
tokens in this statement is n") %>.
would result in the text "The number of tokens in this statement is 9." being displayed in the browser. The code snippet above calls the hypothetical countTokens(String) method that was shown in the declaration section previously. To count the number of tokens in the statement, a literal copy of the statement is passed to the method. In the code snippet above, the method call returned an int value, which was printed to the clients browser. Here is the same expression using XML style:
The number of tokens in this statement is
<jsp:expression>
countTokens("The number of tokens in this statement
is n")
</jsp:expression>.
Any legal Java expression can be used with an expression element. An expression could contain a method call, as shown above, or a literal expression such as '2 + 2', or an expression using Java variables or keywords such as 'v instanceof Vector', or any combination of these. Notice also that because declarations and scriptlets contain Java code, the lines of Java code must be terminated with a semicolon. Expressions, however, will not necessarily be legal code statements (but they will be valid expressions), so they do not need a terminating semicolon.
Comments You can use standard HTML comments within the JSP and those comments will appear in the page received by the client browser. Standard HTML comments have this form:
<!-- This comment will appear in the client's browser >
You can also include JSP-specific comments that use this syntax:
<%-- This comment will NOT appear in the client's
browser %>
JSP comments will not appear in the page output to the client.
Next: Template Data >>
More Java Articles
More By Apress Publishing
|
This article is excerpted from chapter three of Beginning J2EE 1.4 From Novice to Professional, written by James L. Weaver, Kevin Mukhar, and Jim Crume (Apress, 2004; ISBN: 1590593413). Check it out at your favorite bookstore today. Buy this book now.
|
|