Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I escape special HTML characters in JSP?

Before I go and create a custom tag or Java method to do it, what is the standard way to escape HTML characters in JSP?

I have a String object and I want to display it in the HTML so that it appears to the user as is.

For example:

String a = "Hello < World"; 

Would become:

Hello &lt; World 
like image 252
Free Wildebeest Avatar asked Jan 24 '09 10:01

Free Wildebeest


People also ask

Can I escape HTML special characters?

You must keep in mind that inside the <> is also html. In that case skipping > will break. If you're only escaping for between tags then you probably only need escape < and &.

How do you escape special characters?

Escape CharactersUse the backslash character to escape a single character or symbol. Only the character immediately following the backslash is escaped. Note: If you use braces to escape an individual character within a word, the character is escaped, but the word is broken into three tokens.


1 Answers

Short answer:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <c:out value="${myString}"/> 

there is another option:

<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> ${fn:escapeXml(myString)} 
like image 51
Slartibartfast Avatar answered Oct 13 '22 09:10

Slartibartfast