JSP Directives: Page, Include & Taglib Directive | What is Directives in JSP? | Check JSP Syntax & Types of Directives with Examples

Are you looking for information about JSP Directives? If Yes, then this tutorial is helpful for you. Today, we have decided to discuss a JSP topic called JSP Directives with Example. Primarily, there are three types of Directives in JSP. They are Page directive, include directive, and taglib directive. Learn all these directives in detail from this page and also get a good grip on what is JSP Directives & Syntax?

What are JSP Directives?

JSP Directives are designed how the container produces the java code while creating the servlet. To set global values within a particular JSP Page, typically directives are used, hence they affect the whole page. The purpose of the directives is to transfer the data to the JSP container. Hence, a directive doesn’t allow any output to the current output stream of the JSP Page.

Syntax of Directives in JSP

The JSP directives have the following common syntax:

<%@ directive { attr = "value" } %>

Also Refer:

Types of Directives in JSP

There are three types of JSP Directives for us to use:

  1. Page Directive
  2. include Directive
  3. taglib Directive

1. Page Directive

To determine the attribute for the JSP Page, the page directive implements a mechanism. Page directive is applied to set the content type of the page along with the language that will be used for scripting.

The syntax of the JSP Page directive:

<%@ page attribute = "value" %>

Properties/Attributes of Page directive

There are 11 attributes of JSP Page Directives these are explained in detail below.

i) language: It represents the scripting languages to be used in scriptlets, expressions, and declarations.

ii) pageEncoding: It determines the character encoding for the JSP Page. if any other no specified, The default value is ISO-8859-1.

iii) isErrorPage: It indicates if this JSP Page is intended to be the URL Target of another JSP Page errorPage. if set to true, then the implicit script language variable exception is available and references the Throwable object from the offending JSP page. If this value is false, then the exception variable isn’t available and will cause a translation error if it is referenced.

JSP Directives Page, Include and Taglib Directive 1

iv) isThreadSafe: It represents the container whether the page is thread-safe with proper synchronization implemented on the page. A value of true indicates that the container may send multiple requests to the page. if the value is false, requests will be queued and sent one at a time.

v) import: It has the same meaning as the import statement in java classes.

JSP Directives Page, Include and Taglib Directive 2

vi) info: It is used to set an informational String that’s incorporated into the page and accessible by using Servlet.getServletInfo() method.

JSP Directives Page, Include and Taglib Directive 3

vii) extends: It provides a mechanism to define a fully qualified class name of the superclass that the container will extend to generate the translated page.

viii) content-type: It defines the character encoding for the JSP Page and the Multipurpose Internet Mail Extension(MIME) types for the response of the JSP page.

JSP Directives Page, Include and Taglib Directive 4

ix) buffer: It specifies the buffering model for writing content to the output stream. The value of none indicates to write directly through to the ServletResponse using its PrintWriter.While specifying a buffer size, it must be in kilobytes(kb).

JSP Directives Page, Include and Taglib Directive 5

x) autoflush: It specifies that the buffered output is flushed or not and the default value of the “autoFlush” attribute is true. When set to true, the output buffer is automatically flushed when the buffer is full. when set to false, this will raise an exception when the buffer is full.

xi) session: It specifies whether this page participates in the HttpSession. If set to true, then the implicit object session is available for access. This object is of type javax.Servlet.http.HttpSession. If set to false, the object isn’t available and attempted access to it will cause an error

2. Include Directive

The Include directive says the container to change the specified resource content inline to the JSP page. This can be either template text or code. In the include directive, the resources defined must be a relative URL, that’s accessible by the container. If the include files alter, then it doesn’t significantly trigger a recompilation of the JSP file. If an include file changes, then the JSP 2.1 specification recompilation hasn’t required.

The syntax of JSP includes directive:

<%@ include file = "resource name"%>

Example of JSP include directive:

JSP Directives Page, Include and Taglib Directive 6

3. Taglib directive

The purpose of the taglib directive is very powerful as it provides a JSP page to run with custom tag libraries(taglib). In a JSP page, the taglib directive states that the page uses a tag library, which uniquely recognizes the tag libraries with the help of a URI and associated a tag prefix with the action in the library. It is also reasonable to have tag files in a directive named /WEB-INF/tags in JSP 2.1

The syntax of JSP taglib directive:

<%@ directive attribute = "value" %>

Example of JSP taglib directive:

JSP Directives Page, Include and Taglib Directive 7