Directives supply directions and messages to a JSP container. The directives provide global information about the entire page of JSP. Hence, they are an essential part of the JSP code. These special instructions are used for translating JSP to servlet code. In this chapter, you will learn about the different components of directives in detail.



In JSP's life cycle phase, the code must be converted to a servlet that deals with the translation phase. They provide commands or directions to the container on how to deal with and manage certain JSP processing portions. Directives can contain several attributes that are separated by a comma and act as key-value pairs. In JSP, directives are described with a pair of <%@ .... %> tags.

The syntax of Directives looks like:

<%@ directive attribute="" %>
There are 3 types of directives:
  1. Page directive
  2. Include directive
  3. Taglib directive

Let us now discuss each of them in detail.

Page Directive

The page directive is used for defining attributes that can be applied to a complete JSP page. You may place your code for Page Directives anywhere within your JSP page. However, in general, page directives are implied at the top of your JSP page.

The basic syntax of the page directive is:

<%@ page attribute = "attribute_value" %>

The XML equivalent for the above derivation is:

<jsp:directive.page attribute = "attribute_value" />

The attributes used by the Page directives are:

  1. buffer: Buffer attribute sets the buffer size in KB to control the JSP page's output.
  2. contentType: The ContentType attribute defines the document's MIME (Multipurpose Internet Mail Extension) in the HTTP response header.
  3. autoFlush: The autofill attribute controls the behavior of the servlet output buffer. It monitors the buffer output and specifies whether the filled buffer output should be flushed automatically or an exception should be raised to indicate buffer overflow.
  4. errorPage: Defining the "ErrorPage" attribute is the correct way to handle JSP errors. If an exception occurs on the current page, it will be redirected to the error page.
  5. extends: extends attribute used for specifying a superclass that tells whether the generated servlet has to extend or not.
  6. import: The import attribute is used to specify a list of packages or classes used in JSP code, just as Java's import statement does in a Java code.
  7. isErrorPage: This "isErrorPage" attribute of the Page directive is used to specify that the current page can be displayed as an error page.
  8. info: This "info" attribute sets the JSP page information, which is later obtained using the getServletInfo() method of the servlet interface.
  9. isThreadSafe: Both the servlet and JSP are multithreaded. If you want to control JSP page behavior and define the threading model, you can use the "isThreadSafe" attribute of the page directive.
  10. Language: The language attribute specifies the programming language used in the JSP page. The default value of the language attribute is "Java".
  11. Session: In JSP, the page directive session attribute specifies whether the current JSP page participates in the current HTTP session.
  12. isELIgnored: This isELIgnored attribute is used to specify whether the expression language (EL) implied by the JSP page will be ignored.
  13. isScriptingEnabled: This "isScriptingEnabled" attribute determines if the scripting elements are allowed for use or not.

Include Directive

The JSP "include directive" is used to include one file in another JSP file. This includes HTML, JSP, text, and other files. This directive is also used to create templates according to the developer's requirement and breaks the pages in the header, footer, and sidebar.

To use this Include Directive, you have to write it like:

<%@ include file = "relative url" >

The XML equivalent of the above way of representation is:

<jsp:directive.include file = "relative url" />

Example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding = "ISO-8859-1"%>
<%@ include file="directive_header_code.jsp" %>
  
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
        <title>Include Directive Example</title>
    </head>
    <body>
        <p>This file includes a header file named directive_header_code.jsp</p>
    </body>
</html>

In the above example of JSP code, a JSP header file is being added to the current JSP file using "include directive".

Taglib Directive

The JSP taglib directive is implemented to define a tag library with "taglib" as its prefix. Custom tag sections of JSP use taglib. JSP's taglibdirective is used as standard tag libraries.

To implement taglib, you have to write it like this:

<%@ taglib uri="uri" prefix="value"%>

Example:

<%@ taglib uri = "http://www.example.com/custlib" prefix = "w3tag" %>
<!DOCTYPE html>
<html>
    <body>
        <mytag: hi/>
    </body>
</html>


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram