JSP Directives

This article is a series of JSP Tutorial. In this article, we will learn what is JSP directive and what are available JSP directives with an example.

Directives are instructions to the JSP container that describe what code should be generated.
The syntax of directives:
<%@ directive-name [attribute="value" attribute="value" ...] %>
Note: Zero or more spaces, tabs, and newline characters can be after the opening <%@ and before the ending %>, and one or more whitespace characters can be after the directive name and between attributes/value pairs. The only restriction is that the opening <%@ tag must be in the same physical file as the ending %> tag.
There are three standard directives available in all compliant JSP environments:
  1. page Directive
  2. include Directive
  3. taglib Directive
In this article, we will walk through the overview of each of these directives. In the next articles, we will discuss in-depth of each directive.

1. The page Directive

The page directive is used to specify attributes for the JSP page as a whole.

The Syntex of page Directive

<%@ page [attribute="value" attribute="value" ...] %>
There are several attributes, which are used along with page Directive and these are –
  • import
  • contentType
  • extends
  • info
  • buffer
  • language
  • isELIgnored
  • isThreadSafe
  • autoFlush
  • session
  • pageEncoding
  • errorPage
  • isErrorPage
Read each attribute with examples at JSP page Directive article.

2. The include Directive

The JSP include directive is used to include the contents of any resource it may be JSP file, HTML file or text file. The include directive includes the original content of the included resource at page translation time (the JSP page is translated only once so it will be better to include static resource).
The include directive merges the contents of another file at translation time into the .jsp file.

The syntax of include directive

<%@ include file="filename" %>
where filename is an absolute or relative pathname interpreted according to the current servlet context.

A simple example using the include directive

<%@ include file="/header.html" %>
<%@ include file="/doc/legal/disclaimer.html" %>
<%@ include file="sortmethod" %>
Read more about include directive with a complete example at JSP include directive.

3. The taglib Directive

The taglib directive makes custom actions available in the current page through the use of a tag library.

The syntax of the taglib directive

<%@ taglib uri="tagLibraryURI" prefix="tagPrefix" %>
where the attributes are those listed here:
  • tagLibraryURI - The URL of a Tag Library Descriptor.
  • tagPrefix - A unique prefix used to identify custom tags used later in the page.

A simple example using the taglib directive

For example, if the following directive is used,
<%@ taglib uri="/tlds/FancyTableGenerator.tld" prefix="ft" %>
and if FancyTableGenerator.tld defines a tag named table, then the page can contain tags of the following type
<ft:table>
...
</ft:table>
Let's explore more about each JSP directive in-detail in upcoming articles. In the next article, we will learn JSP page directive with an example.

Comments