JSP Directives
What are JSP Directives?
JSP directives provide global information about an entire JSP page to the JSP container. They affect the overall structure of the generated Servlet. Directives use the syntax: <%@ directive attribute="value" %>
There are three types of JSP directives:
- page — Defines page-level attributes
- include — Includes another file at translation time
- taglib — Declares a tag library for use in the page
Page Directive
The page directive defines attributes that apply to the entire JSP page. Common attributes:
| Attribute | Default | Description |
|---|---|---|
| language | java | Scripting language (always "java") |
| contentType | text/html;charset=ISO-8859-1 | MIME type and character encoding |
| pageEncoding | ISO-8859-1 | Character encoding of the JSP page |
| import | — | Java classes/packages to import |
| session | true | Whether the page participates in HTTP sessions |
| errorPage | — | URL of error page to forward to on exception |
| isErrorPage | false | Whether this page is an error page (enables exception object) |
| buffer | 8kb | Output buffer size (none or Nkb) |
| isThreadSafe | true | Whether the JSP is thread-safe |
<%-- Page directive with multiple attributes --%>
<%@ page language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="java.util.*, java.text.SimpleDateFormat"
session="true"
errorPage="error.jsp"
buffer="16kb"
%>
<!DOCTYPE html>
<html>
<body>
<%
// Using imported classes
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String formattedDate = sdf.format(now);
%>
<p>Formatted Date: <%= formattedDate %></p>
<p>Session ID: <%= session.getId() %></p>
</body>
</html>
<%-- Error page (error.jsp) --%>
<%@ page isErrorPage="true" %>
<html><body>
<h2>Error Occurred!</h2>
<p>Message: <%= exception.getMessage() %></p>
</body></html>
Include and Taglib Directives
The include directive (<%@ include file="..." %>) includes the content of another file at translation time. The included file's content is merged into the current JSP before compilation. This is a static include — changes to the included file require recompilation.
The taglib directive (<%@ taglib uri="..." prefix="..." %>) declares a tag library (like JSTL) for use in the page.
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%-- Include directive: static include at translation time --%>
<%@ include file="header.jsp" %>
<%-- Taglib directive: declare JSTL core library --%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<html>
<body>
<h2>Main Content</h2>
<%-- Using JSTL c:if tag --%>
<c:if test="${not empty param.name}">
<p>Hello, ${param.name}!</p>
</c:if>
<%-- Using JSTL fmt tag --%>
<fmt:formatDate value="${now}" pattern="dd/MM/yyyy"/>
<%@ include file="footer.jsp" %>
</body>
</html>
<%-- header.jsp - included statically --%>
<header style="background:#333;color:#fff;padding:10px;">
<h1>My JSP Website</h1>
<nav>
<a href="index.jsp" style="color:#fff;margin-right:10px;">Home</a>
<a href="about.jsp" style="color:#fff;margin-right:10px;">About</a>
<a href="contact.jsp" style="color:#fff;">Contact</a>
</nav>
</header>
Ready to Level Up Your Skills?
Explore 500+ free tutorials across 20+ languages and frameworks.