ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Writing JSPs in XML using JSP 1.2

by Stephanie Fesler

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:

JSP document
A JSP document is a JSP page written in XML syntax. It is a namespace-aware document that uses namespaces to identify the core syntax and additional syntaxes introduced to the page through tag libraries.
XML view
An XML view is the mapping between a JSP page, written in JSP syntax or XML syntax, and an XML document that describes it.


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:

  • JSP documents can be passed directly to the container.
  • The XML view of the JSP document can be used to validate the JSP document against a description of the set of valid pages.
  • JSP documents can be manipulated via XML-aware tools.
  • An XML transformation, like XSLT, can be used to generate a JSP document from a textual representation.

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.

JSP Document Elements

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">
    <title> Simple JSP Document </title>
    <h1>Hello World</h1>

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:

  • jsp:root.
  • JSP directive elements.
  • JSP scripting elements.
  • JSP standard action elements.
  • JSP custom action elements.
  • jsp:text elements.
  • Other XML fragments corresponding to template data.

Now let's take a closer look at each of the valid elements within a JSP document.

JSP Root Element

Related Reading

Java and XML, 2nd Ed.Java and XML, 2nd Ed.
By Brett McLaughlin
Table of Contents
Sample Chapter
Full Description
Read Online -- Safari

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">
    JSP page

The 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.

Pages: 1, 2

Next Pagearrow