background image

Including Directives in a JSP Document

<< Declaring Tag Libraries | The jsp:directive.page Element >>
<< Declaring Tag Libraries | The jsp:directive.page Element >>

Including Directives in a JSP Document

You can include the xmlns attribute in any element in your JSP document, just as you can in an
XML document. This capability has many advantages:
It follows the XML standard, making it easier to use any XML document as a JSP document.
It allows you to scope prefixes to an element and override them.
It allows you to use xmlns to declare other namespaces and not just tag libraries.
The books.jspx page declares the tag libraries it uses with the xmlns attributes in the root
element, books:
<books
xmlns:jsp=
"http://java.sun.com/JSP/Page"
xmlns:c=
"http://java.sun.com/jsp/jstl/core"
>
In this way, all elements within the books element have access to these tag libraries.
As an alternative, you can scope the namespaces:
<books>
...
<jsp:useBean xmlns:jsp=
"http://java.sun.com/JSP/Page"
id=
"bookDB"
class=
"database.BookDB"
scope=
"page">
<jsp:setProperty name=
"bookDB"
property=
"database" value="${bookDBAO}" />
</jsp:useBean>
<c:forEach xmlns:c=
"http://java.sun.com/jsp/jstl/core"
var=
"book" begin="0" items="${bookDB.books}">
...
</c:forEach>
</books>
In this way, the tag library referenced by the jsp prefix is available only to the jsp:useBean
element and its subelements. Similarly, the tag library referenced by the c prefix is only available
to the c:forEach element.
Scoping the namespaces also allows you to override the prefix. For example, in another part of
the page, you could bind the c prefix to a different namespace or tag library. In contrast, the jsp
prefix must always be bound to http://java.sun.com/JSP/Page, the JSP namespace.
Including Directives in a JSP Document
Directives are elements that relay messages to the JSP container and affect how it compiles the
JSP page. The directives themselves do not appear in the XML output.
Creating a JSP Document
Chapter 6 · JavaServer Pages Documents
191