background image

Using the jsp:root Element

<< Generate Tags Dynamically | Generating XML Declarations >>
<< Generate Tags Dynamically | Generating XML Declarations >>

Using the jsp:root Element

The jsp:expression element inserts the value of a scripting language expression, converted
into a string, into the data stream returned to the client. A jsp:expression element has no
attributes and its body is the expression. Its syntax is
<jsp:expression> expression goes here </jsp:expression>
Using the jsp:root Element
The jsp:root element represents the root element of a JSP document. A jsp:root element is
not required for JSP documents. You can specify your own root element, enabling you to use
any XML document as a JSP document. The root element of the books.jspx example JSP
document is books.
Although the jsp:root element is not required, it is still useful in these cases:
When you want to identify the document as a JSP document to the JSP container without
having to add any configuration attributes to the deployment descriptor or name the
document with a .jspx extension
When you want to generate, from a single JSP document, more than one XML document or
XML content mixed with non-XML content
The version attribute is the only required attribute of the jsp:root element. It specifies the JSP
specification version that the JSP document is using.
The jsp:root element can also include xmlns attributes for specifying tag libraries used by the
other elements in the page.
The books.jspx page does not need a jsp:root element and therefore doesn't include one.
However, suppose that you want to generate two XML documents from books.jspx: one that
lists books and another that lists magazines (assuming magazines are in the database). This
example is similar to the one in the section
"Including Directives in a JSP Document" on
page 191
. To do this, you can use this jsp:root element:
<jsp:root
xmlns:jsp=
"http://java.sun.com/JSP/Page" version="2.0" >
<books>...</books>
<magazines>...</magazines>
</jsp:root>
Notice in this example that jsp:root defines the JSP namespace because both the books and the
magazines
elements use the elements defined in this namespace.
Using the jsp:output Element
The jsp:output element specifies the XML declaration or the document type declaration in the
request output of the JSP document.
Creating a JSP Document
The Java EE 5 Tutorial · September 2007
196