JSTL Functions and Formatting: fn:length, fn:toUpperCase, fn:contains, fmt:formatNumber and fmt:formatDate
Raw values in, friendly values out: fn:length counts three students, fn:toUpperCase and fn:contains work on text, and fmt turns 1234.5 into $1,234.50 and a Date into 19 October 2026, in the browser's language.
What you will learn
- Add the fn and fmt taglib lines to a JSP
- Count a list, change case and test text with fn functions
- Format a price as currency and a Date with a pattern
- Explain why the currency symbol follows the browser's language
Before you start
- JSTL Core Tags
- JSP Expression Language Step by Step
Some jobs are too small for a servlet: putting a name in capitals, counting a list, showing a price with a currency symbol. JSTL has helpers for these that sit right in the page. A raw value goes in and a friendly value comes out:
fn:toUpperCaseturns Emma Clark into EMMA CLARK.fmt:formatNumberturns 1234.5 into $1,234.50.fmt:formatDateturns aDateobject into 19 October 2026.
In short, fn works on text and lists, and fmt formats numbers and dates. Both come with the JSTL jars from JSTL core tags.
The servlet sets raw values
package org.studyeasy.web;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
// students for the fn: page, a price and a date for the fmt: page
@WebServlet({"/functions", "/formatting"})
public class StudentServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setAttribute("students", List.of(
new Student("Emma Clark", "[email protected]"),
new Student("Liam Turner", "[email protected]"),
new Student("Olivia Brooks", "[email protected]")));
req.setAttribute("price", 1234.5);
// a fixed date, so the page shows the same thing every day
req.setAttribute("startDate",
new GregorianCalendar(2026, Calendar.OCTOBER, 19).getTime());
String page = req.getServletPath().substring(1);
System.out.println(page + " -> " + page + ".jsp");
req.getRequestDispatcher("/WEB-INF/views/" + page + ".jsp").forward(req, resp);
}
}
A list of three students (name and email), a raw price and a fixed start date, so the page shows the same thing every day. The servlet doesn't format anything. It sets the values and forwards.
Three fn functions
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<%@ taglib prefix="fn" uri="jakarta.tags.functions" %>
<%-- STEP 1: three fn: functions, used inside ${...} --%>
<!DOCTYPE html>
<html>
<head><title>Functions</title></head>
<body>
<h1>Students: ${fn:length(students)}</h1>
<c:forEach var="student" items="${students}">
<p>${fn:toUpperCase(student.name)}
<c:if test="${fn:contains(student.email, '@school.edu')}">(school email)</c:if>
</p>
</c:forEach>
</body>
</html>
Students: 3
EMMA CLARK (school email)
LIAM TURNER
OLIVIA BROOKS (school email)
- One more taglib line, prefix
fn, URIjakarta.tags.functions. fn:length(students)counts the list: 3.- Inside
c:forEach,fn:toUpperCase(student.name)prints each name in capitals. fn:contains(student.email, '@school.edu')is true for Emma and Olivia, so they get the label. Liam's email is at example.com, so he doesn't.
Functions are used inside ${...}, unlike the c: and fmt: tags.
Two fmt tags
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="fmt" uri="jakarta.tags.fmt" %>
<%-- STEP 2: fmt: tags turn raw values into what people read --%>
<!DOCTYPE html>
<html>
<head><title>Formatting</title></head>
<body>
<h1>Formatting</h1>
<p>Raw price: ${price}</p>
<p>Price: <fmt:formatNumber value="${price}" type="currency" /></p>
<p>Start date: <fmt:formatDate value="${startDate}" pattern="d MMMM yyyy" /></p>
</body>
</html>
Formatting
Raw price: 1234.5
Price: $1,234.50
Start date: 19 October 2026
The first line prints the raw value as it is. formatNumber with type="currency" adds the symbol, the thousands separator and two decimals. formatDate with pattern="d MMMM yyyy" gives the day, the full month name and the year.
Who decides $ or €?
The values never change, but the output does. fmt reads the language the browser sends. US English gives the dollar sign, which is why you usually see it. With the browser set to English (UK) it becomes the pound sign, and with German it's the euro sign, with the dot and comma swapping places and the month coming out in German.
Common mistakes
- Deleting the
fmttaglib line. The server doesn't know thefmt:tags, so they reach the browser untouched, and the price and the date disappear from the page. - Forgetting that
fnandfmteach need their own taglib line, besides the one forc. - Formatting prices in the servlet as strings. Keep raw numbers and dates in Java, and format them in the page, where the browser's language is known.
Quick recap
| Helper | Gives |
|---|---|
fn:length(students) | 3 |
fn:toUpperCase(name) | EMMA CLARK |
fn:contains(email, '@school.edu') | true or false |
fmt:formatNumber type="currency" | $1,234.50 |
fmt:formatDate pattern="d MMMM yyyy" | 19 October 2026 |
There are more functions and options than these five. You don't need to memorise them; know they exist and ask an AI assistant for the exact one.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Servlets and JSP: What a Servlet Container Does and How a JSP Becomes a Servlet
A JSP looks like HTML with a little Java, yet Tomcat runs it as a servlet. See what a servlet container does for each request, what the generated hello_jsp.java looks like, what a .war holds, and Tomcat vs TomEE.
JSP Scripting: Expressions, Scriptlets, Declarations, Comments and Includes
The JSP tags you'll read in older Java web apps: expressions, scriptlets, declarations, page imports, two kinds of comment, and the include directive versus the jsp:include action, each shown with what Tomcat turns it into.
JavaBeans and JSP Scopes: Page, Request, Session and Application
A Student bean filled from a form with jsp:useBean and setProperty, then one Counter class kept in four scopes. Three visits, two browsers and an include page show exactly who sees which number.
JSP Expression Language Step by Step: ${...} for Beans, Maps, Lists, Params and Scopes
Expression Language prints a value in a JSP with ${...} and no Java on the page. Read a Student bean, a price map, a tag list and ?city=Boston, run small checks, and see the order EL searches the scopes.
