Lecture 31: JSP Directives, Standard Actions & Custom Tag Libraries

BMC201 - Web Technology

Mr. Prashant Kumar Nag

2026-03-24

Lecture 31

JSP Directives, Standard Actions & Custom Tag Libraries

Week 10 | Unit III: JSP
BMC201 - Web Technology
Mr. Prashant Kumar Nag, Assistant Professor

Learning Objectives


  • Explain JSP directives: page, include, and taglib
  • Use JSP standard actions such as jsp:include and jsp:forward
  • Pass parameters with jsp:param
  • Understand why tag libraries (JSTL/custom tags) are preferred over scriptlets
  • Build modular JSP pages using reusable fragments

What Are JSP Directives?


Directives provide translation-time instructions to the JSP container.

Main directives: - page - include - taglib

Page Directive Example


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.time.LocalDate" %>
<%@ page errorPage="error.jsp" %>

Use contentType and encoding to avoid rendering issues.

Include Directive vs Action


<%@ include file="header.jsp" %>
<jsp:include page="header.jsp" />
  • Include directive: static include at translation time
  • jsp:include: dynamic include at request time

Common JSP Standard Actions


Action Use
<jsp:include> Include another resource dynamically
<jsp:forward> Forward current request
<jsp:param> Send parameters to included/forwarded resource
<jsp:useBean> Locate/create JavaBean
<jsp:setProperty> Set bean property
<jsp:getProperty> Print bean property

Forward with Parameters


<jsp:forward page="result.jsp">
  <jsp:param name="status" value="success" />
</jsp:forward>

Forward keeps the same request/response objects.

Tag Libraries and JSTL


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:if test="${not empty userName}">
  <h3>Hello, ${userName}</h3>
</c:if>

JSTL and EL make JSP cleaner than scriptlet-based code.

Custom Tag Library Concept


Custom tags let teams reuse UI logic and formatting behavior.

Benefits: - Better readability - Reusable components - Less duplicated JSP code

flowchart LR
  A[Tag File / Tag Handler] --> B[Taglib Declaration]
  B --> C[Use Custom Tag in JSP]
  C --> D[Reusable View Logic]

Mini Modular JSP Case


Structure a page with fragments:

  1. header.jsp
  2. menu.jsp
  3. content.jsp
  4. footer.jsp

Compose using jsp:include for runtime flexibility.

Common Mistakes to Avoid


  • Mixing include directive and include action without understanding timing
  • Forgetting taglib declaration before JSTL usage
  • Forwarding after committing output to response
  • Overusing scriptlets where JSTL/EL is enough
  • Hardcoding repeated page fragments in many JSP files

Summary


  • Directives control translation-time behavior
  • Standard actions provide runtime JSP operations
  • Tag libraries improve readability and maintainability
  • Modular JSP structure is ideal for exam and project quality

Questions

Thank You

Any doubts on directives, actions, or tag libraries?