<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ page import="java.util.ArrayList" %> <%@ page import="java.util.Date" %> <%! // Sample data for demonstration private java.util.List getSampleBooks() { java.util.List books = new java.util.ArrayList(); books.add("The Pragmatic Programmer"); books.add("Clean Code"); books.add("Design Patterns"); books.add("Effective Java"); books.add("Java: The Complete Reference"); return books; } %> <% // Set up sample data for taglib demonstration request.setAttribute("bookList", getSampleBooks()); request.setAttribute("currentDate", new Date()); request.setAttribute("userCount", 42); request.setAttribute("serverName", "Gumdrop"); %> Taglib Example - Gumdrop JSP

Taglib Example

← Back to JSP Examples

Note: This JSP demonstrates taglib directive processing and custom tag recognition by Gumdrop's JSP implementation. To fully execute JSTL tags, you would need to add the JSTL JAR files to your WEB-INF/lib directory.

This JSP demonstrates:

  • Taglib directive processing
  • Custom tag recognition and code generation
  • Tag Library Descriptor (TLD) resolution
  • Interaction with Gumdrop's TaglibRegistry

Taglib Directives

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

These directives register tag library prefixes that Gumdrop's JSP implementation will recognize and process during code generation.

JSTL Core Tags (Demonstration)

Conditional Logic

<c:if test="\${not empty serverName}">
    <p>Server: <c:out value="\${serverName}"/></p>
</c:if>

Generated Output (if JSTL were available):

Server: <%= request.getAttribute("serverName") %>

Iteration

<c:forEach var="book" items="\${bookList}" varStatus="status">
    <p>\${status.index + 1}. <c:out value="\${book}"/></p>
</c:forEach>

Generated Output (if JSTL were available):

<% java.util.List books = (java.util.List) request.getAttribute("bookList"); for (int i = 0; i < books.size(); i++) { String book = books.get(i); %>

<%= (i + 1) %>. <%= book %>

<% } %>

Variable Setting and Output

<c:set var="welcomeMsg" value="Welcome to \${serverName} JSP Engine"/>
<p><c:out value="\${welcomeMsg}"/></p>

Generated Output (if JSTL were available):

Welcome to <%= request.getAttribute("serverName") %> JSP Engine

Formatting Tags

<fmt:formatDate value="\${currentDate}" pattern="yyyy-MM-dd HH:mm:ss"/>
<fmt:formatNumber value="\${userCount}" type="number"/>

Generated Output (if JSTL were available):

<% Date currentDate = (Date) request.getAttribute("currentDate"); java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); %>

Formatted Date: <%= sdf.format(currentDate) %>

Formatted Number: <%= request.getAttribute("userCount") %>

How Gumdrop Processes Taglibs

  1. Taglib Directive Processing: When parsing, Gumdrop extracts taglib directives and registers prefix-to-URI mappings in the JSPPage.
  2. TLD Resolution: The TaglibRegistry resolves taglib URIs to Tag Library Descriptor (TLD) files in WEB-INF/lib JARs or WEB-INF/ directory.
  3. Custom Tag Recognition: During parsing, custom tags (like <c:if>) are recognized based on registered prefixes.
  4. Code Generation: The JSPCodeGenerator creates Java code that instantiates tag handler classes and calls their lifecycle methods.
  5. Tag Handler Integration: Generated servlet code properly initializes PageContext and manages tag handler lifecycle (doStartTag, doEndTag, release).

To Enable Full JSTL Support

To make these tags fully functional, you would need to:

  1. Download JSTL JAR files (jstl-1.2.jar, standard-1.1.2.jar)
  2. Place them in your WEB-INF/lib/ directory
  3. Restart Gumdrop to reload the web application

Gumdrop's JSP implementation would then:

  • Find the TLD files inside the JSTL JARs
  • Generate appropriate Java code for tag instantiation
  • Handle tag attributes and body content
  • Manage tag handler lifecycle properly

Taglib Architecture

Gumdrop's taglib support includes:

  • TaglibRegistry: Manages taglib URI-to-TLD mappings
  • TldParser: SAX-based parser for Tag Library Descriptors
  • TagLibraryDescriptor: In-memory representation of TLD metadata
  • Custom Tag Code Generation: Generates proper tag handler instantiation and lifecycle management

Gumdrop JSP Engine - Full JSP 2.0+ Taglib Support