Tutorials Logic, IN info@tutorialslogic.com

JSP Custom Tags SimpleTag, TLD File

Reusable View Tags

Jakarta Pages custom tags package reusable view behavior behind a tag name. Simple tag handlers extend SimpleTagSupport and implement doTag; tag files offer a JSP-based alternative. Use tags for presentation concerns such as formatting or controlled layout, not database access or domain decisions.

View Vocabulary

A custom tag lets a team express repeated UI patterns as readable tags: alerts, buttons, cards, permission blocks, or status badges.

  • Keep JSP pages close to HTML.
  • Remove scriptlets from views.
  • Use tags for reusable presentation behavior.

Tag Files and Handlers

Tag files are good for markup-heavy fragments. SimpleTag handlers are Java classes for behavior-heavy tags.

  • Use attributes for configuration.
  • Use body content for wrappers.
  • Document required attributes in the TLD.

Production Boundaries

Custom tags should render or format. They should not query the database, open connections, or enforce business workflows.

  • Controllers prepare data.
  • Tags render data.
  • Services own business rules.

Tag Contract

Declare attributes with clear required and runtime-expression rules in a TLD or tag file. Escape untrusted values for their output context and decide whether the tag evaluates its body once, conditionally, or not at all. A tag handler may be reused by the container, so do not retain request-specific mutable state beyond one invocation.

Prefer JSTL and EL for standard iteration, conditions, and formatting. Build a custom tag only when it gives several views one stable, testable presentation contract. Keep generated markup accessible and avoid a tag that hides large page-level navigation or business workflows.

Tag File Example

A tag file under WEB-INF/tags is compiled by the container and is useful when the reusable behavior is mostly markup. Attribute directives define the public contract; jsp:doBody renders caller-supplied body content.

Alert Tag File and Use

Alert Tag File and Use
<%-- /WEB-INF/tags/alert.tag --%>\n<%@ tag body-content="scriptless" %>\n<%@ attribute name="type" required="true" %>\n<div class="alert alert-${type}" role="status">\n  <jsp:doBody />\n</div>\n\n<%-- page.jsp --%>\n<%@ taglib prefix="ui" tagdir="/WEB-INF/tags" %>\n<ui:alert type="success">Profile saved.</ui:alert>

The tag owns the wrapper while the page supplies the message body. A production tag library should map an approved finite set of variants rather than accepting an arbitrary class fragment.

SimpleTag Lifecycle

For a Java handler, extend SimpleTagSupport, expose bean-style setters for declared attributes, and write output through getJspContext().getOut() inside doTag(). Invoke getJspBody().invoke(writer) only when the body is permitted and should run.

Containers may pool or reuse handler instances. Reset per-invocation state and never store request data in static fields. A handler that writes user text must escape for HTML, attribute, URL, or JavaScript context as appropriate.

  • Package the handler class and map its name, class, attributes, and body-content in the TLD.
  • Use rtexprvalue when an attribute may receive an EL expression at request time.
  • Test generated markup, missing required attributes, empty body, and untrusted text.

Permission Button Tag

Permission Button Tag
<app:actionButton permission="USER_DELETE" label="Delete" url="/users/delete" />
Before you move on

Tag Contract Review

5 checks
  • Choose tag files or handlers by purpose.
  • Define attributes clearly.
  • Keep database access out of tags.
  • Define body evaluation and runtime-expression behavior.
  • Test accessibility and escaping in generated markup.

Tag Design Failures

  • Database work in doTag

    Load data in a controller or service and pass a view-ready value.
  • Request state retained

    Keep handler fields invocation-safe and clear mutable state.
  • Unescaped attribute output

    Encode for the exact output context before writing markup.
  • Custom tag duplicates JSTL

    Use the standard tag library for ordinary loops, conditions, and formatting.

Try this next

Build a View Tag

0 of 2 completed

  1. Create a tag file that maps three approved states to text and CSS classes. Reject or fall back for unknown states.
  2. Write a SimpleTag that conditionally invokes its body and test both paths. Do not retain the body result between requests.

JSP Custom Tags Questions Learners Ask

No. They belong to the view layer and should render data prepared elsewhere.

Next Step
Next Practice

Finish the concept here, then reinforce it with hands-on coding, interview prep, or a tool that matches the topic.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.