Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To limit the text printed by a JSP, shorten the Java String before calling out.print. The example below caps output at 120 UTF-16 code units, safely handles a null value and avoids an index-out-of-range error when the string is shorter. If “characters” means Unicode code points or visible symbols, use Unicode-aware counting instead.

Truncate the string before printing it

Read the value, choose a nonnegative limit, clamp the substring end index to the string’s length, then print the shortened value:

<%
String value = (String) request.getAttribute("text");
int limit = 120;
String shown = value == null
        ? ""
        : value.substring(0, Math.min(value.length(), limit));
out.print(shown);
%>

Replace "text" and 120 with the attribute name and limit your page needs. The Math.min call prevents an exception if the string is shorter than the limit. The example treats null as an empty string; use a different null-handling policy if your application requires one. If the limit comes from a request parameter, validate it and enforce an application-appropriate maximum before using it; also reject or handle negative values.

Know what Java means by “characters”

The example counts UTF-16 code units, because that is what Java’s String.length() returns. Some Unicode characters outside the Basic Multilingual Plane use two code units, so a limit of 120 units can end after fewer than 120 Unicode code points—and plain substring truncation can split a surrogate pair. Oracle documents this behavior in the Java SE 26 String API and the Java Language Specification.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If the limit is in Unicode code points

Count code points and convert that count to a UTF-16 end offset before taking a substring:

int codePointLimit = 120;
int end = value.offsetByCodePoints(
        0, Math.min(value.codePointCount(0, value.length()), codePointLimit));
String shown = value.substring(0, end);

Apply the same null and nonnegative-limit checks as in the JSP example. This avoids cutting a valid supplementary character in half.

Rank #2
Sale
Murach's Java Servlets and JSP (3rd Edition): Java Programming Book for Web Development with Tomcat, NetBeans IDE, MySQL, JavaBeans & MVC Pattern - Guide to Building Secure Applications
  • Series: Murach: Training & Reference
  • Paperback: 758 pages
  • Language: English
  • ISBN-10: 1890774782, ISBN-13: 978-1890774783
  • Product Dimensions: 8 x 1.7 x 10 inches, Shipping Weight: 3.4 pounds

If the limit is in visible characters

A user-perceived symbol may consist of multiple code points—for example, a base letter and a combining mark. If the requirement is a number of visible symbols, neither String.length() nor code-point counting necessarily matches it; use grapheme-cluster-aware segmentation and truncate at a cluster boundary.

Do not confuse string truncation with JSP buffering

JSP’s implicit out variable is a JspWriter. The page directive’s buffer and autoFlush settings control response-output buffering and what happens when the buffer fills; they do not impose a maximum length on an individual string. The Jakarta Pages 3.1 specification says that with buffer="none", output is written directly through to the response’s PrintWriter: Jakarta Pages 3.1 specification.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use per-value truncation when you need a maximum text length. Change buffering settings only when you need to control how response output is buffered or flushed. The JspWriter API includes methods such as getBufferSize(), getRemaining(), isAutoFlush(), and flush() for managing or inspecting that buffer; they are not per-string character limits.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Keep output encoding separate from truncation

Truncating a string does not make it safe to insert into HTML. If the value can come from a user or another untrusted source, encode it for its destination context when rendering. Choose the truncation unit—UTF-16 code units, code points, or grapheme clusters—based on what the limit is intended to mean.

Quick Recap

SaleBestseller No. 2
Murach's Java Servlets and JSP (3rd Edition): Java Programming Book for Web Development with Tomcat, NetBeans IDE, MySQL, JavaBeans & MVC Pattern - Guide to Building Secure Applications
Murach's Java Servlets and JSP (3rd Edition): Java Programming Book for Web Development with Tomcat, NetBeans IDE, MySQL, JavaBeans & MVC Pattern - Guide to Building Secure Applications
Series: Murach: Training & Reference; Paperback: 758 pages; Language: English; ISBN-10: 1890774782, ISBN-13: 978-1890774783
$40.62
Bestseller No. 4
SaleBestseller No. 5
Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
Used Book in Good Condition
$15.41
Best Value
Sale
Java Servlet & JSP Cookbook
  • Used Book in Good Condition

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.