fn:escapeXml() JSTL Function

fn:escapeXml() JSTL Function Example explains about Escaping Special Characters In JSTL.

Consider an example where you need to escape XML markup and display exactly what we used.

fn:escapeXml() JSTL function will escape all the characters that will be interpreted as XML markup.

java.lang.String escapeXml(java.lang.String)

you can see the below example demonstrating fn:escapeXml() JSTL function 

Required Libraries

You need to download

  1. Tomcat 7
  2. JSTL 1.2

Following jar must be in classpath

  1. jstl-1.2.jar

fn:escapeXml() Example

// JSP - JSTL Escaping Special Characters

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>JSTL fn:escapeXml</title>
</head>
<body>

<%
    // you can also set the values into request scope same as using c:set
    // request.setAttribute("str", ""This is a <a>test</a> String"");
%>


<c:set var="str" value="This is a <a>test</a> String"/>
<p>Using escapeXml() Function:</p>
<p>${fn:escapeXml(str)}</p>

<p>Without Using escapeXml() Function:</p>
<p>${str}</p>

</body>
</html>
Output
Using escapeXml() Function:

This is a <a>test</a> String

Without Using escapeXml() Function:

This is a test String

 






Your email address will not be published. Required fields are marked *