Skip to content
CMO & CTO
CMO & CTO

Closing the Bridge Between Marketing and Technology, By Luis Fernandez

  • Digital Experience
    • Experience Strategy
    • Experience-Driven Commerce
    • Multi-Channel Experience
    • Personalization & Targeting
    • SEO & Performance
    • User Journey & Behavior
  • Marketing Technologies
    • Analytics & Measurement
    • Content Management Systems
    • Customer Data Platforms
    • Digital Asset Management
    • Marketing Automation
    • MarTech Stack & Strategy
    • Technology Buying & ROI
  • Software Engineering
    • Software Engineering
    • Software Architecture
    • General Software
    • Development Practices
    • Productivity & Workflow
    • Code
    • Engineering Management
    • Business of Software
    • Code
    • Digital Transformation
    • Systems Thinking
    • Technical Implementation
  • About
CMO & CTO

Closing the Bridge Between Marketing and Technology, By Luis Fernandez

JSP Includes and Tag Files: Templating That Works

Posted on March 14, 2011 By Luis Fernandez
\n
\n\n\n

Templating in JSP does not need to hurt.
Between includes and tag files you can get clean pages, fast builds, and zero drama.

\n\n\n\n

When people say JSP is messy, they are usually fighting the wrong battle. The quick wins sit right in front of us with JSP includes: the static directive <%@ include file="..." %> and the dynamic action <jsp:include page="..." />. The first one is a copy paste at compile time. The second one is a tiny request inside your page at request time. With static include you share the same translation unit, so EL and declarations are visible. With dynamic include you get isolation and a fresh run of that fragment. On modern Tomcat 7 with Servlet 3.0 and JSP 2.2 both are solid. Pick static for true templates like headers and footers, pick dynamic when the fragment needs its own logic or when you want to swap it based on the request.

\n\n\n\n

Let us ground this. A static include for a layout shell is a joy: <%@ include file="/WEB-INF/jsp/_head.jsp" %>, then your content, then <%@ include file="/WEB-INF/jsp/_foot.jsp" %>. Paths in the directive are resolved at build time, relative to the including JSP, and the whole result compiles as one unit, which means your EL variables and imports are shared. A dynamic include looks like <jsp:include page="/widgets/miniCart.jsp"></jsp:include>. That path is resolved at run time. If it starts with a slash it is from the web app root, otherwise it is relative to the current page. With dynamic include you can pass params with <jsp:param name="..." value="..." />, and the included JSP sees them through request or EL, while still keeping its own page scope. Static gives you fewer moving parts and speed from one compile, dynamic gives you flexibility and reuse across pages without name collisions. Both beat copy paste.

\n\n\n\n

Now the fun part is JSP tag files. A tag file is a JSP that behaves like a tag. You drop it under /WEB-INF/tags and call it from a page. This is gold for reusable UI parts and full page layouts. Define attributes, write regular JSP inside, and expose a body slot. A tiny layout example: create /WEB-INF/tags/layout.tag with <%@ tag body-content="scriptless" %>, then inside do your shell with places for title and the body: <title>${attributes.title}</title> ... <jsp:doBody/>. Use it from a page with <%@ taglib prefix="t" tagdir="/WEB-INF/tags" %> and then <t:layout title="Shop"> ... page content ... </t:layout>. You can accept attributes like title, meta, or a simple boolean for a compact mode. The big win is clarity. No scriptlets, clean EL, and composition that reads like HTML.

\n\n\n\n

There is nuance that saves hours. With the static include your imports and functions are shared, so a method declared in a helper tag is visible to the header. With a dynamic include that method is out of scope, which is often what you want. For content that might be heavy or pulled from a service, wrap it behind <jsp:include> and keep the page lean. If you need a quick switch between two menus based on a role, a dynamic include with a simple EL choice works well. For everything that repeats, push it into a tag file. Need a card component? /WEB-INF/tags/ui/card.tag with attributes like title and image, and the page stays readable: <ui:card title="${p.name}" image="${p.img}">${p.desc}</ui:card>. Pair this with JSTL for loops and conditionals and you get tidy pages that still run inside your servlet container without extra jars. If you are moving to Servlet 3.0 annotations, none of this breaks. It just gets cleaner.

\n\n\n\n

A few tips from the trenches. Use static includes for base layout parts like head and foot and for simple partials that never branch. Use dynamic includes for snippets that need their own lifecycle, like a cart box, a sign in widget, or a listing that takes query params. In dynamic includes the included JSP can read request attributes set by the parent. For tag files favor body-content="scriptless" and keep Java out of them. Expose attributes with <%@ attribute name="title" required="true" %>. If you need optional slots, accept a fragment attribute and render it with <jsp:invoke fragment="actions" />. Example inside a tag file: <%@ attribute name="actions" fragment="true" required="false" %> and then drop <jsp:invoke fragment="actions" /> near the title. This gives you the feel of components without a heavy stack. Search engines are happy, ops is happy, and your views stop repeating themselves.

\n\n\n\n

Keep it simple: static includes for structure, dynamic includes for smart parts, tag files for real reuse.

\n\n\n
\n
Software Architecture Software Engineering

Post navigation

Previous post
Next post
  • Digital Experience (94)
    • Experience Strategy (19)
    • Experience-Driven Commerce (5)
    • Multi-Channel Experience (9)
    • Personalization & Targeting (21)
    • SEO & Performance (10)
  • Marketing Technologies (92)
    • Analytics & Measurement (14)
    • Content Management Systems (45)
    • Customer Data Platforms (4)
    • Digital Asset Management (8)
    • Marketing Automation (6)
    • MarTech Stack & Strategy (10)
    • Technology Buying & ROI (3)
  • Software Engineering (310)
    • Business of Software (20)
    • Code (30)
    • Development Practices (52)
    • Digital Transformation (21)
    • Engineering Management (25)
    • General Software (82)
    • Productivity & Workflow (30)
    • Software Architecture (85)
    • Technical Implementation (23)
  • 2025 (12)
  • 2024 (8)
  • 2023 (18)
  • 2022 (13)
  • 2021 (3)
  • 2020 (8)
  • 2019 (8)
  • 2018 (23)
  • 2017 (17)
  • 2016 (40)
  • 2015 (37)
  • 2014 (25)
  • 2013 (28)
  • 2012 (24)
  • 2011 (30)
  • 2010 (42)
  • 2009 (25)
  • 2008 (13)
  • 2007 (33)
  • 2006 (26)

Ab Testing Adobe Adobe Analytics Adobe Target AEM agile-methodologies Analytics architecture-patterns CDP CMS coding-practices content-marketing Content Supply Chain Conversion Optimization Core Web Vitals customer-education Customer Data Platform Customer Experience Customer Journey DAM Data Layer Data Unification documentation DXP Individualization java Martech metrics mobile-development Mobile First Multichannel Omnichannel Personalization product-strategy project-management Responsive Design Search Engine Optimization Segmentation seo spring Targeting Tracking user-experience User Journey web-development

©2025 CMO & CTO | WordPress Theme by SuperbThemes