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:
<%@ directive attribute1 = "value 1"
attribute2 = "value 2"
...
%>
Code language: HTML, XML (xml)
page Directive
The page directive allows you to define one or more following attributes:
import Option
<em>import="package.class"</em>
<em>import="package.class1,...,package.classN"</em>
Code language: HTML, XML (xml)
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:
<%@ page import="java.util.*" %>
Code language: HTML, XML (xml)
You can use import option multiple times in the page directive.
contentType Option
<em>contentType="MIME-Type"</em>
<em>contentType="MIME-Type; charset=Character-Set".</em>
Code language: HTML, XML (xml)
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:
<%@ page contentType="text/plain" %>
Code language: HTML, XML (xml)
isThreadSafe Option
isThreadSafe="true|false".
Code language: JavaScript (javascript)
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
session="true|false".
Code language: JavaScript (javascript)
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:
<em><%@ page buffer="none" %></em>
<em><%@ page buffer="64kb" %></em>
Code language: HTML, XML (xml)
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:
Servlet.getServletInfo().
Code language: CSS (css)
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:
<%@ page errorPage="error.jsp" %>
Code language: HTML, XML (xml)
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:
<%@ page isErrorPage="true" %>
<html>
<head>
<title>Error Occurred</title>
</head>
<body>
<h1>Error</h1>
An error occurred while processing your request.
<p>
The error message is: <%= exception.getMessage()%>.</p>
Please contact the System Administrator for advices.
</body>
</html>
Code language: HTML, XML (xml)
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:
<%@ include file="relative url" %>
Code language: HTML, XML (xml)
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:
<%@ taglib uri="http://localhost/jsptutorial/taglib/mytaglib" prefix="jsptutorial" %>