There are three main types of JSP constructs: scripting constructs, directives, and actions. Scripting elements enable you to specify Java code that will become part of the resultant servlet. Directives enable you to control the overall structure of the resultant servlet. Actions enable you to control the behavior of the JSP engine. This section introduces scripting constructs.
There are three types of JSP scripting constructs that can be used to insert Java code into a resultant servlet: expressions, scriptlets, and declarations.
A JSP expression is used to insert a Java expression directly into the output. It has the following form:
<%= Java expression %>
The expression is evaluated, converted into a string, and sent to the output stream of the servlet.
A JSP scriptlet enables you to insert a Java statement into the servlet's jspService method, which is invoked by the service method. A JSP scriptlet has the following form:
<%= Java statement %>
A JSP declaration is for declaring methods or fields into the servlet. It has the following form:
<%= Java declaration %>
HTML comments have the following form:
<! HTML Comment >
If you don't want the comment to appear in the resultant HTML file, use the following comment in JSP:
<% JSP Comment %>
Listing 35.1 creates a JavaServer page that displays factorials for numbers from 0 to 10. The program is named Factorial.jsp and saved in c:\jakarta-tomcat-5.5.9\webapps\liangweb . You run it from the URL http://localhost:8080/liangweb/Factorial.jsp , as shown in Figure 35.3.
1 <html> 2 <head> 3 <title> 4 Factorial 5 </title> 6 </head> 7 <body> 8 9 <% for ( int i = ; i <= 10 ; i++) { %> 10 Factorial of <%= i %> is 11 <% = computeFactorial(i) %> <br /> 12 <% } %> 13 14 <%! private long computeFactorial( int n) { 15 if (n == ) 16 return 1 ; 17 else 18 return n * computeFactorial(n - 1 ); 19 } 20 %> 21 22 </body> 23 </html> |
JSP scriptlets are enclosed between <% and %> . Thus
for ( int i = ; i <= 10 ; i++) {, (line 9)
is a scriptlet and as such is inserted directly into the servlet's jspService method.
JSP expressions are enclosed between <%= and %> . Thus
<%= i %> , (line 10)
is an expression and is inserted into the output stream of the servlet.
JSP declarations are enclosed between <%! and %> . Thus
<%! private long computeFactorial( int n) { ... } %>
is a declaration that defines methods or fields in the servlet.
What would be different if line 9 is replaced by the two alternatives shown below? Both work fine, but there is an important difference. In (a), i is a local variable in the servlet, whereas in (b) i is an instance variable when translated to the servlet.
Caution
For JSP, the loop body must be placed inside braces even though the body contains a single statement. It would be wrong to delete the opening brace ( { ) in line 9 and the closing brace ( <% } %> ) in line 12. |
Caution
There is no semicolon at the end of a JSP expression. For example, <%= i; %> is incorrect. But there must be a semicolon for each Java statement in a JSP scriptlet. For example, <% int i = 0 %> is incorrect. |
Caution
JSP and Java elements are case-sensitive, but HTML is not case-sensitive. |