Search code examples
jspjstlel

${fn:escapeXml(var)} does not display the value of var


I am following this tutorial of google appengine. I have a jsp file where I use ${fn:escapeXml(var)} to put the value of String variable var in the html code that will be generated. However, the resultant html code does not put anything at that place. I confirmed it using the view source option in the chrome.

Following is my jsp code:

<%@page import="java.util.List"%>

<%@page import="com.google.appengine.api.datastore.Query"%>
<%@ page import="com.google.appengine.api.datastore.DatastoreService"%>
<%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory"%>
<%@ page import="com.google.appengine.api.datastore.Entity"%>
<%@ page import="com.google.appengine.api.datastore.FetchOptions"%>
<%@ page import="com.google.appengine.api.datastore.Key"%>
<%@ page import="com.google.appengine.api.datastore.KeyFactory"%>


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">



<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<% String var = "asd";  %>
    
value of var:"${fn:escapeXml(var)}"
</body>
</html>

Following is the html code that appears upon clicking "view source" in chrome:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">



<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

    
value of var:"" 
</body>
</html>

Solution

  • Scriptlet variable is not available in EL scopes. Try

    <c:set var="c"><%=var%></c:set>
    value of var:"${fn:escapeXml(c)}"