JSP Tutorial - JSP Directives

JSP directives provide instructions to the JSP engine on how to handle JSP.

A JSP directive affects the structure of the servlet class. It usually has the following form:

<%@ directive attribute="value" %>

Directives can have a list of attributes defined in key-value pairs and separated by commas.

Directive Tags

There are three types of directive tag:

Directive Description
<%@ page ... %> Defines page-dependent attributes, such as scripting language, error page, and buffering requirements.
<%@ include ... %> Includes another file.
<%@ taglib ... %> Declares a tag library containing custom actions which can be used in the page.

The page Directive

The page directive instructs JSP engine on how to process the current JSP page.

We can add page directives anywhere in the JSP page.

By convention, page directives are listed at the top of the JSP page.

The following example shows the basic syntax of page directive:

<%@ page attribute="value" %>

You can write XML equivalent of the above syntax as follows:

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

Page directive Attributes

The following lists of attributes associated with page directive.

Attribute Description
buffer Set a buffering model for the output stream.
autoFlush Set behavior of the servlet output buffer.
contentType Set the character encoding scheme.
errorPage Set the URL of another JSP for Java unchecked runtime exceptions.
isErrorPage Set if this JSP page is used by another JSP page as errorPage.
extends Set a superclass that the generated servlet must extend
import A list of packages as Java import statement in the JSP.
info Provides a string that will be returned from the servlet's getServletInfo() method.
isThreadSafe Set the threading model for the generated servlet.
language Defines the programming language used in the JSP page.
session Specifies whether or not the JSP page is in HTTP sessions
isELIgnored Whether to igore EL expression within the JSP page.
isScriptingEnabled Determines if scripting elements are allowed.

The include Directive

The include directive includes a file into the current JSP page.

This directive merges the content from external files with the current JSP during the translation phase.

You may code include directives anywhere in your JSP page.

The include directive has the following syntax:

<%@ include file="relative url" >

The XML equivalent of the above syntax is as follows:

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

The taglib Directive

We can define custom JSP tags in JavaServer Pages that look like HTML tags.

A tag library is a set of user-defined tags that implement custom behavior.

The taglib directive declares a set of custom tags, identifies the location of the library, and defines a prefix for the custom tags.

The taglib directive follows the following syntax:

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

The XML equivalent of the above syntax is as follows:

<jsp:directive.taglib uri="uri" prefix="prefixOfTag" />

Example of import attribute

The import attribute is used to import class,interface or all the members of a package. It is similar to import keyword in java class or interface.


<%@ page import="java.util.Date" %>
Today is: <%= new Date() %>


Content Type

The contentType attribute defines the MIME(Multipurpose Internet Mail Extension) type of the HTTP response.The default value is "text/html;charset=ISO-8859-1".


<%@ page contentType=application/msword %>
Today is: <%= new java.util.Date() %>


Example of info attribute

info attribute sets the information of the JSP page which is retrieved later by using getServletInfo() method of Servlet interface.


<%@ page info="composed by java2s.com" %>
Today is: <%= new java.util.Date() %>


Example of buffer attribute

The buffer attribute sets the buffer size in kilobytes to handle output generated by the JSP page. The default size of the buffer is 8Kb.


<%@ page buffer="16kb" %>
Today is: <%= new java.util.Date() %>


Example of errorPage attribute

The errorPage attribute is used to define the error page, if exception occurs in the current page, it will be redirected to the error page.


<%@ page errorPage="myerrorpage.jsp" %>

 <%= 100/0 %>


Example of isErrorPage attribute

The isErrorPage attribute is used to declare that the current page is the error page.

The exception object can only be used in the error page.


<%@ page isErrorPage="true" %>

 Sorry an exception occured!<br/>
The exception is: <%= exception %>