The JavaServer Pages Specification, Version 1.2, was released in final form on August 27, 2001. One of the major additions to the new specification is the description of using XML syntax within a JSP. This article explains why writing JSPs in XML syntax is helpful, and the syntax these documents use. Refer to Hans Bergsten's article, JSP 1.2: Great News for the JSP Community: Part 1, to read about other additions/changes in the JSP 1.2 specification. Before we go any further, it is important to define some new terms:
A JSP can now use either traditional JSP syntax or XML syntax within its source file. However, it is important to note that a JSP source file cannot intermix JSP syntax and XML syntax. No matter which type of syntax the JSP uses, it is still saved with a .jsp extension. The container will know the difference because the JSP document has a jsp:root element that cannot exist within traditional JSP syntax. The semantic model of a JSP document is no different than the semantic model of a JSP page. The JSP document generates a response stream of characters from template data and dynamic data. Text or XML fragments are common examples of template data and scripting elements, and standard or custom actions are common examples of dynamic elements. There are many reasons why you would want to use XML syntax in your JSPs. The specification describes the following reasons:
Before the JSP 1.2 specification, there was no support for validating JSPs and little IDE support for manipulating JSPs. Therefore, the new abilities to validate the JSP document and manipulate it via XML-aware tools are great benefits of using XML syntax within JSPs.
Let's begin our discussion of JSP document elements with a simple JSP document, and where better to begin that the traditional "Hello World" example? The following is a JSP document that prints "Hello World" within an HTML page:
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2"> <jsp:text> <html> <head> <title> Simple JSP Document </title> </head> <body> <h1>Hello World</h1> </body> </html> </jsp:text> </jsp:root>
The first step in processing a JSP document is to identify the nodes of the document. All of the textual nodes that are empty are dropped, except jsp:text elements (they are kept verbatim). The resulting nodes are then interpreted based on their element type. Template data is passed directly to the response, or mediated through a standard action or a custom action. A JSP document can contain the following elements:
Now let's take a closer look at each of the valid elements within a JSP document.
All JSP documents have a root: the
jsp:root element. This element signals to the container that it is a JSP document, since this tag is not a valid JSP syntax element. The
jsp:root element is responsible for specifying the appropriate namespaces available to the document, such as the standard namespace that defines the JSP 1.2 syntax, and other tag libraries that define custom actions. The JSP root element has two attributes, the
xmlns and the version attribute. The general syntax of the root element is:
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:prefix1="URI-for-taglib1" xmlns:prefix2="URI-for-taglib2" ...version="1.2">
xmlns attribute is optional and is used to include the standard elements defined in the JSP 1.2 specification and custom actions. Tag libraries are now included in the JSP document using the
xmlns attribute instead of the
taglib directive. The version attribute is a required attribute and it defines the JSP specification the JSP document is being written to, which will be 1.2.
The page directive is used to define a number of page-dependent properties and pass these values to the container. This element must appear at the beginning of the JSP document.
<%@ page page_directive_attr_list %>
<jsp:directive.page page_directive_attr_list />
The include directive is used to substitute text and/or code at JSP translation time. A JSP in either syntax can use the include directive to include another JSP written in either syntax. This element can appear anywhere within a JSP document.
<%@ include file="relativeURL" %>
<jsp:directive.include file="relativeURL" />
An interesting point to make: the XML view of a JSP page does not contain
jsp:directive.include elements; rather, the included file is expanded in place. This is done to simplify validation.
The declaration element is used to declare scripting-level constructs that are available to all other scripting elements. This element does not have any attributes and the body of the element is the declaration(s).
<%! declaration(s) %>
The scriplet element is used to insert fragments of code. This element does not have any attributes and the body of the element is the program fragment.
<% code fragment %>
The expression element is used to describe complete expressions that get evaluated at run-time. This element does not have any attributes and the body of the element is the expression.
<%= expression %>
The standard and custom action elements were defined in XML syntax prior to the 1.2 specification. Just as a reminder, the standard actions include:
The text element is used to enter template data into a JSP document. This element does not have any attributes and the body of the element is the template data. The XML syntax of this element is:
<jsp:text>template data</jsp:text>. When this element is interpreted, the body is passed through to the current value of
out. This element is very similar to the XSLT
JSP documents provide many benefits, including the ability to validate JSPs and to manipulate them with XML-aware tools. They behave the same way a traditional JSP does, by generating a response stream of characters from template data and dynamic data. JSP documents even contain many of the same types of elements, just now in XML syntax. This is one of the great additions to the JSP 1.2 specification, and I think it will play more and more of a role as the specification and developer community mature.
Stephanie Fesler is a BEA Systems expert on implementing various Java 2EE API.
Return to ONJava.com.
Copyright © 2009 O'Reilly Media, Inc.