Lecture 24: JSP Scripting, Directives, Actions & Custom Tags

BMC201 - Web Technology

Mr. Prashant Kumar Nag

2026-03-06

Lecture 24

JSP Scripting, Directives, Actions & Custom Tags

Week 8 | Unit III: Web App Development (Servlets + JSP)
BMC201 - Web Technology
Mr. Prashant Kumar Nag, Assistant Professor

Learning Objectives


  • Use scripting elements and directives
  • Apply JSP action tags
  • Understand custom tags for reuse

Prerequisites


  • Java basics: classes, methods, objects, and exception handling
  • HTTP request-response fundamentals from Unit I/II web context
  • Revision of Lecture 23 before moving into JSP Scripting, Directives, Actions & Custom Tags

Syllabus Mapping


  • Unit III topic focus: JSP Scripting, Directives, Actions & Custom Tags
  • CO alignment: implementation understanding + architecture reasoning
  • Assessment alignment: short definitions + long implementation/design questions

Agenda


  • 5-minute recap from previous lecture
  • Concept deep dive: JSP Scripting, Directives, Actions & Custom Tags
  • Code/configuration walkthrough and output analysis
  • Debug checklist and exam-oriented summary

Introduction


JSP scripting, directives, action tags, and custom tags were introduced to solve two recurring view-layer problems: repeated markup and mixed controller logic in pages.

A maintainable JSP design keeps composition reusable, pushes control logic to servlets, and uses tags/directives to make view pages predictable and easier to update.

Think About It


When one layout fragment changes, how many JSP files should you edit in a good design?

JSP Elements


flowchart TD
    D["Directives"] --> P["Page setup"]
    A["Action Tags"] --> I["Dynamic include/forward"]
    S["Scriptlets/Expressions"] --> R["Runtime values"]
    C["Custom Tags"] --> U["Reusable UI logic"]

  • Directives configure JSP page behavior
  • Action tags enable dynamic composition (jsp:include, jsp:forward)
  • Custom tags reduce duplication and improve maintainability

JSP Directive Types and Use Cases


Directive Purpose Typical Example
page Page-level settings content type, import, error page
include Static include at translation time shared header/footer fragments
taglib Register custom/JSTL tag library reusable presentation logic

Rule of thumb: use directives for setup/composition metadata, not runtime business logic.

Scripting Elements: Where They Fit


Element Syntax Intent Risk if Overused
Declaration <%! ... %> class-level field/method shared mutable state issues
Scriptlet <% ... %> inline Java statements unreadable JSP, logic mixing
Expression <%= ... %> output expression value poor escaping discipline

Modern guideline: keep scriptlets minimal; prefer EL/JSTL/custom tags for readability.

JSP Action Tags in Request Flow


flowchart LR
    C["Controller Servlet"] -->|forward| V["JSP View"]
    V -->|jsp:include| F["Reusable Fragment"]
    V -->|jsp:useBean / jsp:setProperty| B["Bean Binding"]
    V --> H["Final HTML"]

  • jsp:include evaluates target at request time (dynamic content)
  • jsp:forward transfers control to another resource
  • Bean actions connect request data with view-friendly objects

Why Custom Tags Improve JSP Design


Custom tags encapsulate repeated presentation logic into reusable units.

Typical advantages:

  • remove repeated HTML + conditional display blocks
  • centralize formatting rules (badge, status, date rendering)
  • keep JSP pages declarative and easier for review

Example usage in JSP:

<my:badge type="success" text="Active" />
<my:currency value="${order.total}" />

Refactoring Case: From Repetition to Reuse


Before refactor:

  • each page manually writes header, nav, and alert block
  • style or text changes require edits in many files

After refactor:

  • include directive handles static fragments
  • jsp:include handles dynamic fragments
  • custom tags handle repetitive UI patterns

Outcome: fewer defects, faster updates, cleaner view architecture.

JSP Composition Technique Comparison


Technique Evaluation Time Best Use Limitation
include directive (<%@ include ... %>) Translation time Static shared fragments (header/footer) Requires recompilation on change
jsp:include action Request time Dynamic/conditional fragment loading Slight runtime overhead
Custom tag (<my:...>) Request time Reusable UI logic and formatting Requires tag library setup

Practical strategy: static layout via directive include, dynamic content via jsp:include, reusable patterns via custom tags.

Common JSP Composition Pitfalls


  • Pitfall: using scriptlets for controller decisions
    Fix: move decision logic to servlet and pass model attributes

  • Pitfall: confusing static include with dynamic include
    Fix: use directive include for static fragments and jsp:include for runtime fragments

  • Pitfall: duplicating UI patterns across pages
    Fix: create custom tags and enforce reusable fragments

Code Walkthrough


<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/fragments/header.jsp" %>
<%! int count = 0; %>
<% count++; %>
Current visits: <%= count %>
<jsp:include page="/fragments/menu.jsp" />

Memory Booster


Syllabus memory points for JSP Scripting, Directives, Actions & Custom Tags:

  • Core recall: page/include directives and jsp:include
  • Exam compare: Scriptlet-heavy JSP vs reusable tag/action approach
  • Practical anchor: Compose page with include/action and remove duplicated markup

Live Demo


Live implementation for JSP Scripting, Directives, Actions & Custom Tags:

Open Demo: Lecture 24 - JSP Scripting, Directives, Actions & Custom Tags

Demo flow:

  • Compare repeated JSP markup against include/action-based composition
  • Observe dynamic composition with jsp:include
  • Review one UI refactor using reusable custom tag style

Takeaway: reusable JSP composition reduces duplication and maintenance cost.

Resources & References


Structured Debug Checklist


  1. verify the primary API usage for JSP Scripting, Directives, Actions & Custom Tags is correct (imports, method names, config)
  2. check request/bean/session flow and object lifecycle assumptions
  3. inspect server logs for the first exception (not just the final symptom)
  4. reproduce one failing case and one passing case before finalizing fixes

Exam Preparation Questions: Short


  • Define JSP Scripting, Directives, Actions & Custom Tags with one practical use case.
  • Write/identify the key API or construct: page/include directives and jsp:include.
  • Differentiate: Scriptlet-heavy JSP vs reusable tag/action approach.
  • Mention one common implementation error and correction.

Exam Preparation Questions: Long


  • Explain JSP Scripting, Directives, Actions & Custom Tags with architecture/flow and implementation steps.
  • Write a structured answer comparing two approaches used in this topic.
  • Discuss debugging strategy for this topic with likely failure points.

Practice Task


  • Implement: Compose page with include/action and remove duplicated markup.
  • Add console/log output to validate flow step-by-step.
  • Document one bug you encountered and the exact fix.

Checklist


Can you:

  • explain JSP Scripting, Directives, Actions & Custom Tags in your own words?
  • implement a basic example end-to-end?
  • identify and fix one common runtime issue?

Next Lecture


  • Topic: Lecture 25 - Spring Core Basics & Dependency Injection
  • Preparation required: revise this lecture summary and code walkthrough

Questions?

Next: Lecture 25