Nearby lessons
6 of 30JSP - Directives (page, include, taglib)
- Understand the three types of directives
- Compare the include directive (static) vs include action (dynamic)
- Know all 13 page directive attributes
- Use the taglib directive for custom tags
Directives provide general information about the JSP page to the JSP Engine at translation time. This lesson covers the three directive types — page, include, and taglib — with complete examples and a comparison of include directive vs include action.
What Are Directives?
Directives provide general information about the JSP page to the JSP Engine. They are translation-time instructions — processed when the JSP is translated into a servlet, not at runtime.
Syntax:
Three Types of Directives
| Directive | Purpose |
|---|---|
| page | Defines page-specific attributes (imports, session, error handling, etc.) |
| include | Includes another file's content at translation time (static include) |
| taglib | Makes custom tag libraries available to the JSP |
I) page Directive
The page directive specifies overall properties of the JSP page to the JSP Engine. Think of it as telling the Engine "about yourself."
You can use the page directive any number of times, anywhere in the JSP.
page Directive Attributes
The page directive has 13 attributes:
| Attribute | Purpose | Default |
|---|---|---|
import | Import classes and interfaces | Auto-imports: java.lang.*, javax.servlet.*, javax.servlet.http.*, javax.servlet.jsp.* |
session | Participate in session management | true |
contentType | MIME type of the response | text/html |
isELIgnored | Disable EL processing | false (JSP 2.0+) |
isThreadSafe | Thread safety control | true |
isErrorPage | Mark as error page | false |
errorPage | Redirect to error page on exception | none |
language | Scripting language | java |
extends | Parent class for generated servlet | none |
buffer | Output buffer size | 8kb |
autoFlush | Auto-flush buffer when full | true |
info | Page description string | none |
pageEncoding | Character encoding for the page | ISO-8859-1 |
import Attribute
Import classes and interfaces from a package — similar to the Java import statement.
Multiple Import Packages
You can import multiple packages using either separate import attributes or a comma-separated list:
Import Rules
Within the same JSP:
- You cannot take any attribute multiple times with different values
- You can take any attribute multiple times with the same value
- Exception: the
importattribute can be repeated with different values
Auto-Imported Packages
The following packages are automatically imported — you do not need to import them explicitly:
java.lang.*javax.servlet.*javax.servlet.http.*javax.servlet.jsp.*
session Attribute
By default, the session object is available in every JSP. To disable it:
session Attribute Details
- If you do not declare session, or declare it as
true, the generated servlet contains:HttpSession session = null; session = pageContext.getSession(); - If you declare
session="false", that code is not generated. - Allowed values: any case-insensitive
trueorfalsestring. - Any other value causes a translation-time error.
contentType Attribute
Specify the MIME type of the response:
isELIgnored Attribute
Expression Language (EL) was introduced in JSP 1.2. Its main goal is to remove Java code from JSPs.
isELIgnored="true"— EL syntax is treated as plain text, not processed.isELIgnored="false"— EL syntax is processed and prints its value.
Default: true in JSP 1.2, but false from JSP 2.0 onwards.
isThreadSafe Attribute
By default, a JSP page can be accessed by multiple threads simultaneously — it is not thread-safe.
isThreadSafe="true"— no protection, processes any number of requests simultaneously.isThreadSafe="false"— the generated servlet implementsSingleThreadModel, processing only one request at a time.
II) include Directive
When several JSPs share the same code, we can separate that common code into a separate file and include it wherever needed.
The include directive includes the content of another file at translation time. This is called static include.
III) include Action vs include Directive
There are two ways to include content:
| Include Directive (Static) | include Action (Dynamic) |
|---|---|
<%@ include file="second.jsp" %> | <jsp:include page="second.jsp" /> |
1 attribute: file | 2 attributes: page, flush |
| Content included at translation time | Response included at runtime |
| Single servlet generated for both JSPs | Separate servlets generated |
| Code sharing possible | Code sharing not possible |
| Higher performance | Lower performance |
| May not include latest version | Always includes the latest version |
| Use for: logos, headers, footers (unchanging content) | Use for: dynamic content that changes frequently |
IV) taglib Directive
The taglib directive makes custom tags available to the JSP:
taglib Attributes
prefix— a short name used to invoke custom tags (e.g.<mine:myTag/>)uri— the location of the TLD file, which points to the tag handler class
- Directives are translation-time instructions to the JSP Engine
- The include directive is static (translation time); jsp:include is dynamic (runtime)
- The page directive has 13 attributes controlling imports, session, EL, threading, and more
- The taglib directive makes custom tags available to a JSP