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

Custom Tags in JSP: When and How to Write Them

Posted on September 23, 2006 By Luis Fernandez

Custom Tags in JSP are not magic. They are just a cleaner way to say the same things we keep repeating in views. Today I want to cover when they are worth writing and how to pull them off without pain. This is day to day stuff from the trenches, not brochure talk.

Dialogue style intro

Dev: Our JSPs look like a yard sale. Scriptlets, includes, copy paste everywhere.

Lead: You are describing a view that needs custom tags. Wrap the repeats into reuse and call it a day.

Dev: Should I jump to JSF or stick with JSP taglibs

Lead: If your app is already JSP with JSTL and maybe Struts tags, stay put. Write focused tags. JSF is another trip, not a band aid. Custom tags solve repetition now.

Evidence section

What recent projects showed

We shipped two mid sized apps on Tomcat and one on WebSphere. We kept business code far from JSP and leaned on JSTL 1 point 1 for control flow. Still, three pain points kept coming back.

  • Repeated view patterns. Currency formatting with the same flags. Date boxes with the same HTML and errors. Table headers with the same sort links.
  • Permission checks. We called the same helper for canView or canEdit across many pages.
  • Ajax friendly markup. We added the same script hooks for DWR and Prototype again and again.

Every time we introduced a small taglib, JSP size shrank and diff noise dropped. One team measured it. The account section lost 27 percent of JSP lines after moving formatting and permission checks into tags. Reviews got quicker because the noise was gone. The server load did not move in a way anyone could see on the graphs. We profiled anyway and saw no hot spots in tag handlers when we kept them thin.

On the other side, a team that wrote a big all purpose tag to render whole forms paid for it. The tag turned into a second framework. New rules kept sneaking in. Bugs got sticky. The take away is simple. Small tags win. Big meta tags become pets you need to feed.

Implementation notes

Pick the lightest level first. Before writing Java classes, try a tag file in WEB INF tags. A tag file is just JSP with attributes. It is perfect for markup heavy helpers like a date input, a sortable header, or a safe link that checks permissions. Tag files play nice with EL and are quick to tweak with a designer sitting next to you.

When you need logic that is not markup, move to a SimpleTag handler in Java. Keep it short. Accept simple attributes. Avoid keeping state around. If the tag builds large strings or reads data, let a service do it and have the tag call that service. The tag is just a thin delegate.

  • TLD and packaging. Put the TLD in META INF of your jar and drop the jar in WEB INF lib. Give your taglib a clear URI and a short prefix. Avoid clashing with JSTL or framework prefixes.
  • Attributes. Favor EL friendly types like String, Boolean, and numbers. If an attribute is optional, give it a sane default. Keep attribute names short and clear.
  • Expression Language. Expose attributes for value and for display details. Let the page pass beans through EL and keep scriptlets out.
  • Design time help. Document tags in the TLD. A short description per attribute saves future you.
  • Reuse first. Check JSTL, the Apache taglibs, and your framework tags. Do not write a date formatter if fmt already covers it.

If your app uses Struts, you already have tags for forms and errors. Use your custom tags to fill gaps. For Spring MVC, pair JSTL with tag files and you are covered for most screens. If you are adding Ajax with DWR or Dojo, tags can help print consistent hooks and progressive enhancement without scattering script glue.

Risks

Do not rebuild a framework. A tag that tries to handle every case becomes slow to read and slow to change. Split it.

  • Performance. Tag files compile to servlets. If you nest them deeply on big lists, you pay in method calls. Profile with a real data set. Cache pure formatting when you can at a higher level.
  • Thread safety. Tag handlers are reused. Do not keep request data in fields. Use local variables and rely on attributes only.
  • Pooling surprises. Some containers pool tag instances. Reset all fields in doTag or release. Tests catch this fast.
  • Debug pain. When a tag hides too much, stepping through is slow. Keep tags thin and log inputs when developing.
  • Portability. Different containers are picky about TLD locations and validation. Ship the TLD in the jar and avoid container specific tricks.
  • Naming collisions. Prefixes are cheap. Use a team prefix to avoid clashing with third party taglibs.

One more thing. Security checks inside tags are fine for view level decisions, like hiding a button. They are not a replacement for checks in controllers or services. Treat them as cosmetics over a solid gate.

Graceful exit

Here is the short guide I give teams when they ask for a rule of thumb about Custom Tags in JSP: When and How to Write Them.

  • Write a tag file when you see the same markup pattern three times.
  • Write a SimpleTag in Java when the work is logic heavy and the output is small.
  • Use JSTL first. Only fill the gaps, do not compete with it.
  • Keep tags tiny. One job. One clear name. One screen worth of concern.
  • Ship with docs. TLD descriptions and a one page readme in the jar.

You do not need a new stack to clean up messy JSP. Start with the tools you already have. A handful of well chosen tags can lower noise, make reviews pleasant, and let you focus on behavior. If you try this on your messiest screen next week, send me the before and after. I bet the team will not want to go back.

Final note. If you are about to write a tag and feel it turning into a new mini framework, stop and split it. Your future self will say thanks.

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