JSP Syntax Basics – Directives
Introduction
In JavaServer Pages (JSP), directives are special instructions that provide information to the JSP container about how a JSP page should be translated and executed. Directives do not produce any output directly; instead, they control page-level configuration, resource inclusion, and tag library usage.
Directives are processed during the translation phase of the JSP life cycle.
Syntax of JSP Directives
JSP directives use the following syntax:
<%@ directiveName attribute="value" %>
<%@and%>mark a directivedirectiveNamespecifies the type of directive- Attributes define configuration options
Types of JSP Directives
There are three main JSP directives:
- Page Directive
- Include Directive
- Taglib Directive
1. Page Directive
Purpose
The page directive is used to define page-level settings such as content type, error handling, session management, buffering, and imported classes.
Syntax
<%@ page attribute="value" %>
Common Attributes of Page Directive
language
Specifies the scripting language used in JSP (default is Java).
<%@ page language="java" %>
contentType
Defines the MIME type and character encoding of the response.
<%@ page contentType="text/html; charset=UTF-8" %>
import
Imports Java packages or classes for use in JSP.
<%@ page import="java.util.*, java.sql.*" %>
session
Specifies whether the JSP page participates in an HTTP session.
<%@ page session="true" %>
buffer
Defines the size of the output buffer.
<%@ page buffer="8kb" %>
autoFlush
Specifies whether the buffer should be flushed automatically.
<%@ page autoFlush="true" %>
errorPage
Specifies the JSP page that handles runtime errors.
<%@ page errorPage="error.jsp" %>
isErrorPage
Indicates whether the current JSP is an error-handling page.
<%@ page isErrorPage="true" %>
isThreadSafe
Controls multithreading behavior.
<%@ page isThreadSafe="true" %>
2. Include Directive
Purpose
The include directive is used to include the contents of another file (JSP, HTML, or text) into the current JSP page at translation time.
Syntax
<%@ include file="header.jsp" %>
Key Characteristics
- Static inclusion
- Included content becomes part of the JSP page
- Changes in included file require recompilation
Use Cases
- Common headers and footers
- Navigation menus
- Reusable page fragments
3. Taglib Directive
Purpose
The taglib directive is used to define custom tag libraries, such as JSTL, for use in JSP pages.
Syntax
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Key Elements
uriidentifies the tag libraryprefixis used to access tags in JSP
Example Usage
<c:out value="${username}" />
Difference Between Include Directive and Action Include
| Feature | Include Directive | <jsp:include> |
|---|---|---|
| Time of inclusion | Translation time | Request time |
| Type | Static | Dynamic |
| Recompilation | Required | Not required |
| Performance | Faster | Slightly slower |
Key Points About JSP Directives
- Directives affect the entire JSP page
- Processed only once during translation
- Do not generate output directly
- Improve configurability and modularity
Conclusion
JSP directives form the foundation of JSP syntax by providing instructions that control how a JSP page is processed and executed. The page directive manages page-level behavior, the include directive supports static content reuse, and the taglib directive enables the use of custom tags and JSTL. Proper use of JSP directives leads to cleaner code, better organization, and more maintainable JSP-based web applications.
