Directive Elements in JSP

Introduction to Directive Elements

Directives control the processing of JSP page. Directive tag instructs the web container at the time of translating a JSP page into corresponding Servlet page.

There are 3 types of directives in JSP:
i. page directive
ii. include directive
iii. taglib directive

Syntax of Directive:
<%@ directive attribute= "value" %>

page directive

The page directive allows applying different attributes to be added in JSP.  These attributes give special processing information to the JSP container.

Following are the page directive attribute:

AttributeDescription
importImport list of packages, classes an interfaces in servlet class definition.
Example: <%@ page import = "java.util.Date" %>
extendsUsed to extend the parent class that will be generated by Servlet.
Example: <%@ page extends = "mypackage.DemoClass"%>
languageDefines which scripting language to be used in the page.
Example: <%@ page language = "value"%>
sessionSpecifies the JSP page participating in an HTTP session.
Example: <%@ page session = "true"%>
bufferSpecifies a buffer model to handle output stream generated by JSP page.
Example: <%@ page buffer = "4kb"%>
autoFlushSpecifies that buffer should be flushed automatically. The default value of autoFlush attribute is ‘true’.
Example: <%@ page autoFlush = "false"%>
infoSets the information of the JSP page which is retrieved later by using getServletInfo( ) method.
Example: <%@ page info = "Given by Surendra Maurya"%>
contentTypeSets the content of the JSP page.
Example: <%@ page contentType="text/html"%>
isThreadSafeIt is used to define the threading model for the JSP page which is generated by Servlet because JSP and servlet both are thread safe.
Example: <%@ page isThreadSafe="false"%>
errorPageDefine the error page, if any error generates in current page, it will be redirected to the error page.
Example: <%@ page errorPage="erroropage.jsp"%>
isErrorPageDefines whether the current JSP page is error page or not.
Example: <%@ page isErrorPage="true" %>
isELIgnoredSpecifies whether the expression will be evaluated or not in JSP page.
Example: <%@ page isELIngored="true" %>

include directive

  • The include directive is used to insert a file like JSP file, html file or text file. It will parse the JSP elements during translation phase.
  • The main advantage of the include directive is code reusability functionality.
Syntax:
<%@ include file="resourceName" %>

Example

<html>
    <head>
        <title>Include Directive</title>
    </head>
<body>
    <%@ include file = "header.jsp">
</body>
</html>

taglib directive

  • It is used to define a tag library that contains many custom tags. JSP technology supports the development of reusable components called custom actions.
  • The custom tags can be customized through passing the attribute from invoking page. It can be accessed by all the available objects of JSP page.
Syntax:
<%@ taglib prefix="prefixOfTag" uri="uriOfTagLibrary" %>

Example

<html>
    <head>
        <title>Taglib directive</title>
    </head>
    <%@ taglib prefix="myTag" uri="http://www.tutorialride.com/java-technologies.htm" %>
<body>
     Welcome,  <myTag:userName / >   
</body>
</html>