Wednesday, April 27, 2011

JSP: Declarations

Declarations are the third and final scripting element available for use in JSP. A
declaration is used like a scriptlet to embed code in a JSP, but code embedded by
a declaration appears outside of the _jspService()method. For this reason code
embedded in a declaration can be used to declare new methods and global class
variables, but caution should be taken because code in a declaration is not
thread-safe, unless made so by the writer of that code.

JSP:Expressions

Expressions provide an easy method of sending out dynamic strings to a client.
An expression must have a start, <%=, end, %>, and an expression between. An
expression element differs in syntax from a scriptlet by an equal sign that must
appear immediately after the start. Expressions always send a string of text to a
client, but the object produced as a result of an expression does not have to
always end up as an instance of a String object.Any object left as the result of an
expression automatically has its toString() method called to determine the
value of the expression. If the result of the expression is a primitive, the prim-
itive’s value represented as a string is used.

JSP: scriptlets

Scriptlets
Scriptlets provide a method for directly inserting bits of Java code between
chunks of template text. A scriptlet is defined with a start ,<%, an end, %>, with
code between. Using Java, the script is identical to normal Java code but without
needing a class declaration or any methods. Scriptlets are great for providing low-
level functionality such as iteration, loops, and conditional statements, but they
also provide a method for embedding complex chunks of code within a JSP.
For many reasons complex scriptlets should be avoided. This is mainly due to
the fact that the more scriptlets are used the harder it is to understand and
maintain a JSP. In this chapter most of the scriptlet examples are purposely kept
simple. This aids in directly demonstrating the core functionality of JSP, but it is
also done so that examples do not appear to encourage heavy use of scriptlets. As
an introduction, Listing 2 provides a simple JSP that loops to produce multiple
lines of text. Looping is accomplished the same as in Java but by placing the
equivalent Java code inside scriptlet elements. Loop.jsp
Listing 2 
<html>
<head>
<title>Loop Example</title>
</head>
<body>
<% for (int i=0; i<5;i++) { %>
Repeated 5 Times.<br>
<% } %>
</body>
</html>
Save  Loop.jsp in the base directory of the jspbook Web Application and
browse to http://127.0.0.1/jspbook/Loop.jsp. The page shows up with the
statement, “Repeated 5 Times.”, repeated five times.

output
Repeated 5 Times
Repeated 5 Times
Repeated 5 Times
Repeated 5 Times
Repeated 5 Times


It is important to note that the contents of the scriptlet did not get sent to a
client. Only the results of the scriptlet did. This is important because it shows that
scriptlets are interpreted by a container and that code inside a scriptlet is not by
default shared with visitors of the JSP.
A JSP may contain as many scriptlets as are needed, but caution should be
taken not to overuse scriptlets. Scriptlets make a JSP very hard to maintain and
are not easily documented; for example, tools like javadoc do not work with JSP

JSP Syntax and Semantics


JSP is not governed by the syntax and semantics defined by the Java 2 specifica-
tions. Translated JSP source code is just Java,but when you author a JSP, you abide
instead by the rules laid out in the JSP specification. With each release of the JSP
specification, these rules grow, and they cannot be easily summed by a few sen-
tences. The majority of this chapter focuses on explaining the current syntax and
semantics of JSP. Much of the functionality found in JSP is taken directly from the
underlying Servlet API .
Everything in a JSP is broken down into two generic categories called elements
and template data. Elements are dynamic portions of a JSP. Template data are the
static bits of text between. Template data are easily categorized as they are all the
text arbitrarily placed on a JSP and meant to be directly sent to a client. Elements
are easily categorized as custom actions, tags, and the content allowed to be in
between as defined by the JSP specifications.
The concept of elements and template data is important to understand as it
dictates when, where, and what text will do when placed in a JSP. This chapter has
yet to introduce any elements, but it has shown a use of template text. The
HelloWorld.jsp example was entirely template text. The corresponding Servlet
generated from HelloWorld.jsp treated this text as static content and sent it as
the content of a response. While HelloWorld.jsp only had one big chunk of tem-
plate text, more complex JSP follow the same rule. Any chunk of template text is
taken and sent directly to a client as it appears on the JSP. Elements on the other
hand are not sent directly to a client. An element is interpreted by a JSP container
and defines special actions that should be taken when generating a response.
Template text does not change, and this little section defines it in total.
Elements are what make JSP dynamic, and elements are further explained
throughout the rest of the chapter. Elements can be broken down into three dif-
ferent categories: scripting elements, directives, and actions. The following self-
named sections explain these elements.
Two Types of Syntax
JSP containers are required to support two different formats of JSP syntax:
normal and XML-compatible. The normal JSP syntax is a syntax designed to be
easy to author. The XML-compatible JSP syntax takes the normal JSP syntax and
modifies it to be XML-compliant
1
. Both syntaxes provide the same functionality,
but the XML-compatible syntax is intended to be more easily used by devel-
opment tools. In the examples of this book the normal JSP syntax is preferred as
it is easily read, understood, and will be familiar if you have used older versions
of JSP.
Just because the XML syntax will not be appearing much in this book’s
examples does not mean it is the lesser of the two syntaxes. The JSP XML syntax
introduced in the JSP 1.2 specification, from a developer’s perspective, is cer-
tainly a hassle to use compared to the regular syntax. This is largely due to the
complexity and strict enforcement of syntax the JSP 1.2 XML syntax had. JSP 2.0
remedies the problem by providing a much more flexible XML syntax. Later on
in the chapter this new, more flexible XML syntax is further explained.
Scripting Elements
The simplest method of making a JSP dynamic is by directly embedding bits of
Java code between blocks of template text by use of scripting elements. In theory
JSP does not limit scripting elements to only those containing Java code, but the
specification only talks about Java as the scripting language, and every container
by default has to support Java. Examples in this book use Java as a scripting lan-
guage. There are three different types of scripting elements available for use in
JSP: scriptlets, expressions, and declarations.