Mark Johnson
Sun Microsystems created JSP (JavaServer Pages) technology for building
Web pages on the fly on a server. Sun's original dynamic content
technology was "servlets" -- server-side plugin Java programs that
produce dynamic content usually using HTML. Many servlets, particularly
early ones, were mostly "print" statements, which printed out HTML and
substituted dynamic information where appropriate. Microsoft's ASP
(Active Server Pages technology turned that idea on its head. An ASP is
an HTML page containing special markup that embeds pieces of scripts.
Though a good idea, ASPs were Microsoft-specific so Sun created
JavaServer Pages to compete with ASPs in the cross-platform arena,
providing freedom of choice to systems designers.
A JavaServer Page (or, simply "JSP page") combines HTML tags with HTML-
tag-like markup to produce HTML (or XML) when run on a server. For
example, a JSP page containing code like this:
<TABLE><TR>
<% for (int i = 1; i < 5; i++) { %>
<TD><%= i %></TD>
<% } %>
</TD></TABLE>
...will produce HTML that looks like this:
<TABLE><TR>
<TD>1</TD>
<TD>2</TD>
<TD>3</TD>
<TD>4</TD>
</TR></TABLE>
JSP pages, for all their usefulness, have some problems. First, they're
very difficult to maintain when they're not written cleanly. Second,
they're seldom written cleanly; therefore, maintenance is usually an
issue for JSPs of nontrivial complexity. To make matters worse, you
might have noticed something about the JSP page fragment above: it's
not even well-formed markup. Don't bother trying to parse something
that looks like <%= i %> with an XML parser. It just won't work. The
XML specification doesn't allow such a string to form an element.
Why is this a problem? Controlling the structure of the document
contents is one of the benefits of validating marked-up documents using
DTDs. You can eliminate a whole class of problems with markup (though,
of course, not all problems) by validating the document with a DTD.
However, structural safeguards that a validating XML parser could
provide to a JSP page aren't available to it, because the JSP notation
shown above can't possibly be well-formed.
Fortunately, the designers of JSP technology have provided an alternate
XML syntax for JSP pages. Every XML-like, non-well-formed markup
structure (like the <%= %> structure used for expressions), can also be
expressed as a well-formed XML tag. JSP's designers also defined a DTD
for that markup, so you can create well-formed *and* valid JSP pages,
improve the quality of your documents, and simplify the maintenance
process.
The code sample above, in JSP XML notation, looks like this:
<TABLE><TR>
<jsp:scriptlet>
for (int i = 1; i < 5; i++) {
</jsp:scriptlet>
<TD><jsp:expression>i</jsp:expression></TD>
<jsp:scriptlet>}</jsp:scriptlet>
</TD></TABLE>
This JSP page may not be particularly readable, but it *is* a well-
formed XML fragment and it *can* be validated against a DTD. As for the
readability problem, that's a concern of JSPs in general rather than
the notation, and can be addressed with other techniques. At least this
form provides JSP developers with a way to create JSP pages that can be
validated against a DTD.
Each application's designer that uses JSP technology will need to
choose between the original JSP notation's pseudo-markup (which is the
most widely-known, and more concise), and the XML-based alternate
notation. However, knowing you have the choice is nice when choosing to
use JSP pages.