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:
| Attribute | Description |
|---|---|
| import | Import list of packages, classes an interfaces in servlet class definition. Example: <%@ page import = "java.util.Date" %> |
| extends | Used to extend the parent class that will be generated by Servlet. Example: <%@ page extends = "mypackage.DemoClass"%> |
| language | Defines which scripting language to be used in the page. Example: <%@ page language = "value"%> |
| session | Specifies the JSP page participating in an HTTP session. Example: <%@ page session = "true"%> |
| buffer | Specifies a buffer model to handle output stream generated by JSP page. Example: <%@ page buffer = "4kb"%> |
| autoFlush | Specifies that buffer should be flushed automatically. The default value of autoFlush attribute is ‘true’. Example: <%@ page autoFlush = "false"%> |
| info | Sets the information of the JSP page which is retrieved later by using getServletInfo( ) method. Example: <%@ page info = "Given by Surendra Maurya"%> |
| contentType | Sets the content of the JSP page. Example: <%@ page contentType="text/html"%> |
| isThreadSafe | It 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"%> |
| errorPage | Define the error page, if any error generates in current page, it will be redirected to the error page. Example: <%@ page errorPage="erroropage.jsp"%> |
| isErrorPage | Defines whether the current JSP page is error page or not. Example: <%@ page isErrorPage="true" %> |
| isELIgnored | Specifies 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.
<%@ 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.
<%@ 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>


