Java Reference
In-Depth Information
To get a better understanding of declarations, let's take the previous string declaration and
actually use it to create a JSP document. The sample document would look similar to the fol-
lowing code snippet:
<HTML>
<BODY>
<%! String name = new String(“BOB”); %>
</BODY>
</HTML>
When this document is initially requested, the JSP code is converted to servlet code and the
previous declaration is placed in the declaration section of the generated servlet. The
Declarations section of the generated servlet would look similar to the following code snippet:
// begin [file=”D:\\Declarations.jsp”;from=(3,3);to=(3,37)]
String name = new String(“BOB”);
// end
Expressions
JSP expressions are elements in a scripting language that are evaluated with the result being
converted to a java.lang.String . After the string is converted, it is written to the current out
JspWriter object.
JSP expressions are evaluated at HTTP request time, with the resulting string being inserted at
the expression's referenced position in the .jsp file. If the resulting expression cannot be con-
verted to a string then a translation time error will occur. If the conversion to a string cannot be
detected during translation, a ClassCastException will be thrown at request time. The syntax
of a JSP expression is as follows:
14
<%= expression %>
A code snippet containing a JSP expression is shown here:
Hello <B><%= getName() %></B>
To get a better understanding of expressions, let's take this snippet and insert it into a simple
JSP document. The sample document would look similar to the following code snippet:
<HTML>
<BODY>
<%! String name = new String(“BOB”); %>
<%! public String getName() { return name; } %>
 
Search WWH ::




Custom Search