Free tools Windows power users keep installed
One-click scans. No signup required.
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.
#1 Best Overall
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
- 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.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errorsUse 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.
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
Best Value
Rank #4
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.

