views:

28

answers:

1

When I get from MyBean some text, for example, <p>Hello world</p> and include it into jsp via jsp:getProperty in resulting html page it will be &lt;p&gt;Hello world&lt;/p&gt; I could guess that tomcat's team did this due to security reasons but it's not behaviour that I want to get.

Q:how to get needed behaviour?

UPD:

@Jörn Horstmann

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:useBean id="xs" class="bean.XS" scope="application"></jsp:useBean>
<html>
  <head><title>Test</title></head>
  <body>
  <c:out value="${xs.yoba}" escapeXml="true"/> <br>
  <c:out value="${xs.yoba}" escapeXml="false"/> <br>
  <c:out value="&lt;p&gt;HELLO WORLD&lt;/p&gt;" escapeXml="true"/> <br>
  <c:out value="&lt;p&gt;HELLO WORLD&lt;/p&gt;" escapeXml="false"/>
  </body>
</html>

Getter fun:

public String getYoba(){
    return "&lt;p&gt;HELLO WORLD&lt;/p&gt;"  ;
}

Resulting html:

<html> 
  <head><title>Test</title></head> 
  <body> 
  &amp;lt;p&amp;gt;Год выпуска: 20 июля 2010&amp;lt;/p&amp;gt; <br> 
  &lt;p&gt;Год выпуска: 20 июля 2010&lt;/p&gt; <br> 
  &amp;lt;p&amp;gt;HELLO WORLD&amp;lt;/p&amp;gt; <br> 
  &lt;p&gt;HELLO WORLD&lt;/p&gt;
  </body> 
</html>

I want:

<html> 
  <head><title>Test</title></head> 
  <body> 
  <P>HELLO WORLD</P>
  </body> 
</html>
A: 

You can use the jstl c:out tag with escapeXml set to false to solve this:

<c:out value="${myBean.property}" escapeXml="false"/>

Edit:

When your getter already returns escaped HTML you would have to first unescape it, for example using Apache Commons Lang StringEscapeUtils:

public String getYoba(){
    return StringEscapeUtils.unescapeHtml("&lt;p&gt;HELLO WORLD&lt;/p&gt;");
}
Jörn Horstmann
@responded in post
Jeriho