Contextual output encoding for Java 8+ to help prevent XSS. Version 1.6.0 provides a zero-dependency core, String and Writer APIs, and optional javax/Jakarta JSP adapters.
Use Encode.forContextName(rawValue), choosing the method for the parser that receives the value. Every facade method has String and Writer overloads. Supply trusted surrounding syntax and quotes, and encode raw values once at each actual parser boundary. Account for template engines that already escape output.
Do not place untrusted data in tag names, attribute names, JavaScript expressions, or other contexts the encoder does not support. Prefer safe DOM text APIs and avoid dynamic event handlers.
Encode.forHtml(value) covers HTML text and ordinary quoted text attributes. For more specific contexts use forHtmlContent or forHtmlAttribute. It does not validate URLs or make JavaScript event-handler code safe.
Example JSP HTML text:
<p><%= Encode.forHtml(message) %></p>
Use forHtmlContent for text between HTML tags, including textarea content.
<p><%= Encode.forHtmlContent(message) %></p>
<textarea><%= Encode.forHtmlContent(message) %></textarea>
For direct Writer output:
out.write("<p>");
Encode.forHtmlContent(out, message);
out.write("</p>");
Always quote HTML attribute values. Use forHtmlAttribute for ordinary text attributes:
<input value="<%= Encode.forHtmlAttribute(message) %>">
This is not an event-handler encoder or a URL validator. Validate a URL before encoding it for href or src. Prefer quoted attributes over the legacy forHtmlUnquotedAttribute API.
Use forUriComponent for one raw query parameter name or value; assemble the URL with trusted delimiters, then encode for its enclosing context.
String url = "/search?q=" + Encode.forUriComponent(query) + "&page=1";
out.write("<a href='");
Encode.forHtmlAttribute(out, url);
out.write("'>Search</a>");
Do not pass an entire URL or already percent-encoded input to forUriComponent.
Encode each raw path segment with Encode.forUriComponent(segment), using trusted slashes between segments. Validate the assembled destination and enforce application-specific path rules. Dot segments such as . and .. require separate handling; component encoding does not remove them.
When embedding the result in HTML, encode the complete validated URL with forHtmlAttribute inside quotes. Component encoding uses UTF-8 percent encoding and %20 for spaces; it is not HTML form encoding.
Validate an entire untrusted URL before output: parse it, allow-list schemes (commonly https and http), and enforce the destination, port, and path restrictions required by the application. Parsing with the Java URI class alone does not establish safety. Decide explicitly whether relative URLs are allowed.
After validation, output that same validated URL with Encode.forHtmlAttribute inside a quoted href or src. Encode link text separately with forHtmlContent.
Deprecated forUri does not validate a URL: forUri("javascript:alert(1)") leaves it unchanged. Use forUriComponent for individual raw components, not whole URLs.
Use forJavaScriptBlock for string content inside an HTML script element, with trusted single or double quotes around the result.
<script>
const message = "<%= Encode.forJavaScriptBlock(message) %>";
</script>
Encode the complete string value where possible. Never insert into an unfinished escape sequence or an already unsafe template. This method is not for HTML attributes, JSON, JavaScript expressions, or script URLs. Version 1.6.0 reduces interior letter escaping while retaining protection against recognized HTML tokens across trusted-text boundaries.
Choose the JavaScript mode by its actual context. forJavaScript handles string data in script blocks, quoted event-handler attributes, and standalone JavaScript; forJavaScriptAttribute is specifically for quoted event-handler attributes; forJavaScriptSource is only for standalone JavaScript resources. Supply JavaScript string quotes, and also quote any enclosing HTML attribute. Prefer separate event listeners over dynamic inline handlers.
Never use these encoders for expressions, identifiers, regular expressions, JSON, or javascript: URLs. Do not insert output after an unfinished JavaScript escape or into an HTML character reference begun by trusted text.
Use forCssString only inside a quoted CSS string. Use forCssUrl for a value inside CSS url(...), after allow-listing its URL scheme and destination. Neither API accepts arbitrary selectors, property names, numeric values, or declarations.
Prefer a separate stylesheet or safe DOM API. CSS embedded in an HTML attribute must also satisfy the enclosing HTML attribute context. Do not use the historical unquoted width example as a pattern.
For a Jakarta JSP container, use org.owasp.encoder:encoder-jakarta-jsp:1.6.0. Do not install the javax adapter alongside it.
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" %>
<%@ taglib prefix="e" uri="owasp.encoder.jakarta" %>
<p>${e:forHtmlContent(param.message)}</p>
<input value="${e:forHtmlAttribute(param.message)}">
Tags also support <e:forHtmlContent value="${param.message}" />. The advanced identifier is owasp.encoder.jakarta.advanced. Taglib identifiers are lookup names, not web pages. Use a compatible JSP/EL container and enable EL deliberately.
For a javax JSP container, use org.owasp.encoder:encoder-jsp:1.6.0. Do not install the Jakarta adapter alongside it.
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" %>
<%@ taglib prefix="e" uri="https://www.owasp.org/index.php/OWASP_Java_Encoder_Project" %>
<p>${e:forHtmlContent(param.message)}</p>
The advanced identifier appends #advanced. Tags require a value attribute and an empty body. Plain JSP EL does not automatically HTML-escape data; do not evaluate encoded results as EL a second time.
Project Leader
Jeremy Long co-leads OWASP Java Encoder and coordinates project maintenance, releases, and security response with Jim Manico.
Project Leader
Jim Manico is the founder of Manicode Security, where he trains developers and organizations in secure coding. He is an OWASP Distinguished Lifetime Member, a Java Champion, and the author of Iron-Clad Java. He leads the OWASP AISVS, Cheat Sheet Series, Java HTML Sanitizer, and Java Encoder projects.
Recognizing key contributors who have made significant impact on this project.
Original Author
Original author of OWASP Java Encoder.