Nearby lessons

6 of 30

JSP - Directives (page, include, taglib)

📌 What You Will Learn
  • 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:

Example01
JCode Cell
1 
2<%@ directiveName attributeName=attributeValue %>
3

Three Types of Directives

DirectivePurpose
pageDefines page-specific attributes (imports, session, error handling, etc.)
includeIncludes another file's content at translation time (static include)
taglibMakes 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.

Example03
JCode Cell
1 
2<%@ page language="java" %>
3<%@ page session="true" %>
4

page Directive Attributes

The page directive has 13 attributes:

AttributePurposeDefault
importImport classes and interfacesAuto-imports: java.lang.*, javax.servlet.*, javax.servlet.http.*, javax.servlet.jsp.*
sessionParticipate in session managementtrue
contentTypeMIME type of the responsetext/html
isELIgnoredDisable EL processingfalse (JSP 2.0+)
isThreadSafeThread safety controltrue
isErrorPageMark as error pagefalse
errorPageRedirect to error page on exceptionnone
languageScripting languagejava
extendsParent class for generated servletnone
bufferOutput buffer size8kb
autoFlushAuto-flush buffer when fulltrue
infoPage description stringnone
pageEncodingCharacter encoding for the pageISO-8859-1

import Attribute

Import classes and interfaces from a package — similar to the Java import statement.

Example05
JCode Cell
1 
2<%@ page import="java.util.*" %>
3<%@ page import="java.io.*" %>
4

Multiple Import Packages

You can import multiple packages using either separate import attributes or a comma-separated list:

Example06
JCode Cell
1 
2<%-- Option 1: separate attributes (works) --%>
3<%@ page import="java.util.*" import="java.io.*" %>
4 
5<%-- Option 2: single attribute (also works) --%>
6<%@ page import="java.util.*, java.io.*" %>
7

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 import attribute 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:

Example09
JCode Cell
1 
2<%@ page session="false" %>
3

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 true or false string.
  • Any other value causes a translation-time error.

contentType Attribute

Specify the MIME type of the response:

Example11
JCode Cell
1 
2<%@ page contentType="application/pdf" %>
3

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.

Example12
JCode Cell
1 
2<%-- EL is processed: output = "User Name: Mallika" --%>
3<%@ page isELIgnored="false" %>
4<h1>User Name: ${param.user}</h1>
5 
6<%-- EL is NOT processed: output = "User Name: ${param.user}" --%>
7<%@ page isELIgnored="true" %>
8<h1>User Name: ${param.user}</h1>
9

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 implements SingleThreadModel, 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.

Example14
JCode Cell
1 
2<%@ include file="second.jsp" %>
3<h1>This is First JSP</h1>
4

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: file2 attributes: page, flush
Content included at translation timeResponse included at runtime
Single servlet generated for both JSPsSeparate servlets generated
Code sharing possibleCode sharing not possible
Higher performanceLower performance
May not include latest versionAlways 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:

Example16
JCode Cell
1 
2<%@ taglib prefix="mine" uri="/WEB-INF/MyTld.tld" %>
3

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
📝 Key Takeaways
  • 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

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3