Directive Tags


The JSP directive tags provide the web container with some special instructions while translating the jsp page into corresponding servlet.

There are 3 types of directive tags:

1. page directive

2. include directive

3. taglib directive


Page Directive Tag

The jsp page directive tag is used to define the attribute that communicates with web container at the time of translation and are applied to the entire jsp page.

Syntax:

    <%@ page attribute = “value” %>


Attributes of Page directive tag

1. import: It is used to import the classes, packages and interface required by the servlet class.

2. contentType: It is used to define the MIME type of the HTTP response.

3. extends: It is used to define the super class that will be inherited by servlet.

4. info: It is used to set the information of a jsp page which can be retrieved by getServletInfo() method.

5. buffer: It is used to handle the output generated by jsp page.

6. language: It is used to specify the language we used in our jsp page.

7. isELIgnored: It is used to enable/disable the Expression Language in our jsp page.

8. isThreadSafe: It is used to declare whether the jsp page is thread-safe or not.

9. isErrorPage: It is used to declare whether the current jsp page represents an error page of another jsp page.

10. errorPage: It is used to define an error page i.e. if an error occurs in the current jsp page then it will be redirected to the error page that it defines.

11. session: It is used to define whether a jsp page is participating in an HTTP session.

12. autoFlush: It is used to define whether the buffered output is flushed automatically.



Include Directive Tag

The jsp include directive tag is used to include the content of other files.

Syntax:

    <%@ include file = “filename” %>


Example: We can create a single header file and then include this file in every page.

    <html>
        <body>
            <%@ include file=”header.html” %>
        </body>
    </html>


Taglib Directive Tag

The taglib directive tag is used to define a tag library that is used by our jsp page.

Java Server Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which provides many core functionalities to our jsp page.

Syntax:

    <%@ taglib prefix=”prefixOfTagLibrary” uri=”uriOfTagLibrary” %>


Prefix is used to distinguish our custom tag from other library custom tags and uri is used to define a unique name for the tag library.

Example: In the following example, we are using our tag named as curDate. In order to use this custom tag, we have to specify the taglib directive in our jsp page.

    <html>
        <body>
            <%@ taglib prefix=”myCustomTag” uri=https://www.tutorialsduniya.com/custom-tags %>
            <myCustomTag:curDate/>
        </body>
    </html>