Whats this "c:" all about?
Categories:
Demystifying the 'c:' Prefix in JSP/JSTL

Explore the 'c:' prefix in JSP and JSTL, understanding its role in core tag library usage, common pitfalls, and best practices for building robust web applications.
If you've worked with JavaServer Pages (JSP) for web development, you've likely encountered the mysterious c:
prefix. This seemingly small detail is fundamental to using the JavaServer Pages Standard Tag Library (JSTL), a collection of custom tags that simplify JSP development. This article will demystify the c:
prefix, explain its purpose, and guide you through its proper implementation.
What is the 'c:' Prefix?
The c:
prefix is a convention used to identify tags belonging to the JSTL Core tag library. JSTL provides several tag libraries, each with a specific function (e.g., core, formatting, SQL, XML). The Core library, identified by c:
, offers general-purpose tags for common tasks like iteration, conditional logic, variable management, and URL manipulation. Using a prefix helps avoid naming conflicts between different tag libraries or custom tags you might define.
flowchart TD A[JSP Page] --> B{Tag Library Declaration} B --> C["<%@ taglib uri=\"http://java.sun.com/jsp/jstl/core\" prefix=\"c\" %>"] C --> D[Use JSTL Core Tags] D --> E["<c:forEach items=\"${myList}\" var=\"item\">"] E --> F["<c:if test=\"${item.active}\">"] F --> G[Render Content] G --> H[Browser]
Workflow of using the 'c:' prefix in a JSP page
Declaring the JSTL Core Library
Before you can use any JSTL Core tags, you must declare the tag library at the top of your JSP page. This declaration tells the JSP container which URI corresponds to which prefix. For the JSTL Core library, the standard URI is http://java.sun.com/jsp/jstl/core
, and the conventional prefix is c
. The declaration looks like this:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
JSP taglib directive for the JSTL Core library
taglib
directive at the very beginning of your JSP file, typically after any page
directives, to ensure the tags are correctly recognized throughout the page.Common JSTL Core Tags and Their Usage
Once declared, you can use JSTL Core tags with the c:
prefix. These tags replace much of the cumbersome Java scriptlet code often found in older JSP applications, leading to cleaner, more readable, and maintainable code. Here are some frequently used c:
tags:
<c:set var="username" value="John Doe" />
<p>Welcome, <c:out value="${username}" /></p>
<c:if test="${userLoggedIn}">
<p>You are logged in.</p>
</c:if>
<c:forEach var="item" items="${shoppingCart}">
<li><c:out value="${item.name}" /> - $<c:out value="${item.price}" /></li>
</c:forEach>
<c:choose>
<c:when test="${score >= 90}">
<p>Grade: A</p>
</c:when>
<c:when test="${score >= 80}">
<p>Grade: B</p>
</c:when>
<c:otherwise>
<p>Grade: C or lower</p>
</c:otherwise>
</c:choose>
Examples of common JSTL Core tags in action
jstl-1.2.jar
and standard-1.1.2.jar
or similar versions) in your web application's WEB-INF/lib
directory. Without these, your application server will not be able to resolve the tag library, leading to deployment errors or runtime exceptions.