Wednesday, April 27, 2011

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.

No comments:

Post a Comment