JSP Directives

JSP directives instruct the JSP container to work with JSP pages. There are three directives in the JSP specification: page, include and taglib. The directives follow the following forms:

page Directive

The page directive allows you to define one or more following attributes:

import Option

Import option allows you to specify which classes or packages are used on the page. for example, you can import all the classes in the package java.util as follows:
You can use import option multiple times in the page directive.

contentType Option

The contentType option allows you specify the MIME type of the output page. The default istext/html. You can also use other values such as text/plain for outputting text format as follow:

isThreadSafe Option

isThreadSafe option allows you to indicate whether the page is processed as thread-safe. The default value of the isThreadSafe is true therefore all JSP pages are considered thread-safe. IfisThreadSafe is set to false, JSP engine ensures that only one thread at a time is executing the current JSP page.

session Option

By using session option, you are telling JSP compiler that you want to use session or not. The default value of the session attribute is true, which indicates that there is an implicit variable called session available.

buffer and autoFlush Options

buffer and autoFlush options let you control the JSP buffering.

You can turn buffer on or off, or even specify the buffer size using buffer option as follow:

 

autoFlush option controls whether the buffer is flushed automatically when it is full. The default value of autoFlush is true means JSP automatically flush the buffer when it is full.

Info Option

Info option allows you to define the description of the servlet. Then you can access this value by calling the method:

extends Option

In some cases, you need to create your own super class of a JSP page and using this supper class in different JSP pages. In order to do so, you need to create a super class first. This supper class typically inherits from the HttpServlet class. And in the JSP pages, you use the extends option to use the supper class.

isELIgnored Option

isELIgnored option is used to disable the evaluation of the expression language (EL). You will learn more about expression language in the later tutorial.

errorPage Option

errorPage option allows you to indicate the error page when an error occurs in the current executing JSP page, for example:

isErrorPage Option

isErrorPage option indicates that the current JSP page can be used as an error page for other JSP pages. When you set isErrorPage equal true, JSP engine creates an implicit exception object which contains the Throwable object that triggers the call to the error page. By using this exception object, you can display error messages, for example:

Include Directive

Include directive lets you to include a file (JSP or HTML) when JSP engine translates the JSP page into a servlet. The syntax of include directive is as follows:

The filename you specify in the include directive is a relative URL. If you provide a file name without associated path, the JSP compiler always assumes that the file is in the same directory as the current JSP page.
Normally the include directive is used to include common sections of a JSP page in a web application or website such as header, navigation bar, footer… to make those sections reusable in every JSP page.

taglib Directive

JSP has a set of standard tags available. JSP also allows you to create new custom tags that look like HTML or XML tags. In this case, you can use taglib directive to use your custom tag in your JSP page. To use the custom tag, you can refer to the custom tag tutorial.

The syntax of using taglib directive is as follows:

  • Was this tutorial helpful ?
  • YesNo