Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
FAQs Support
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

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:

AttributeDefaultDescription
languagejavaScripting language (always "java")
contentTypetext/html;charset=ISO-8859-1MIME type and character encoding
pageEncodingISO-8859-1Character encoding of the JSP page
importJava classes/packages to import
sessiontrueWhether the page participates in HTTP sessions
errorPageURL of error page to forward to on exception
isErrorPagefalseWhether this page is an error page (enables exception object)
buffer8kbOutput buffer size (none or Nkb)
isThreadSafetrueWhether the JSP is thread-safe
Page Directive Examples
<%-- 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.

Include and Taglib Directives
<%@ 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.