Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

JSP and Servlets
  • Servlets and JSP: What a Servlet Container Does and How a JSP Becomes a Servlet
  • Set Up Tomcat 11 and a Maven Web Project in IntelliJ IDEA
  • The Servlet Request Lifecycle: @WebServlet, web.xml, init, doGet and destroy
  • Servlet Request Parameters, and Forward vs Redirect
  • Run a Java Web App in Docker: Tomcat 11 With One Compose Command
  • Sessions, Cookies and Filters in Java Servlets: Log In Once, Stay Logged In
  • Servlets and JSP: What a Servlet Container Does and How a JSP Becomes a Servlet
  • JSP Scripting: Expressions, Scriptlets, Declarations, Comments and Includes
  • JavaBeans and JSP Scopes: Page, Request, Session and Application
  • JSP Expression Language Step by Step: ${...} for Beans, Maps, Lists, Params and Scopes
  • JSTL Core Tags: c:set, c:out, c:if, c:choose and c:forEach
  • JSTL Functions and Formatting: fn:length, fn:toUpperCase, fn:contains, fmt:formatNumber and fmt:formatDate
  • Maintaining and Migrating Legacy JSP With AI: Explain the Page, Refactor to a DAO, Servlet and JSTL, Then Move It to React
  • MVC and the Front Controller With Servlets and JSP
  • Forms With JSP and Servlets: Handle the POST and Validate on the Server
  • Mini App With Servlets and JSP: Login, Logout and a Shared Web Template
  • SQLite and a JDBC Connection Pool in a Java Web App
  • Users CRUD With Servlets and JSP, Part 1: Add and List Through a DAO
  • Users CRUD With Servlets and JSP, Part 2: Update and Delete
  1. Blog
  2. /
  3. JSP and Servlets
  4. /
  5. JSP

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.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026JSP

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:toUpperCase turns Emma Clark into EMMA CLARK.
  • fmt:formatNumber turns 1234.5 into $1,234.50.
  • fmt:formatDate turns a Date object 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, URI jakarta.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 same price 1234.5 and the same date go into fmt. The browser's language picks the output: US English gives the dollar sign, UK English gives the pound sign, German gives the euro sign with the dot and comma swapped and the month in German. price 1234.5same date fmtreads the language en-US: dollar sign ($) en-GB: pound sign (£) German: euro sign (€) dot and comma swap, month in German
You write the tag once; the browser's language picks the symbol and the format.

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 fmt taglib line. The server doesn't know the fmt: tags, so they reach the browser untouched, and the price and the date disappear from the page.
  • Forgetting that fn and fmt each need their own taglib line, besides the one for c.
  • 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

HelperGives
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.

Keep going in JSP and Servlets · 12 of 19

Previous
JSTL Core Tags: c:set, c:out, c:if, c:choose and c:forEach
Next post
Maintaining and Migrating Legacy JSP With AI: Explain the Page, Refactor to a DAO, Servlet and JSTL, Then Move It to React
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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.

On this page

  • The servlet sets raw values
  • Three fn functions
  • Two fmt tags
  • Who decides $ or €?
  • Common mistakes
  • Quick recap
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.