Not able to display special characters properly in a JSP page

前端 未结 2 1013
耶瑟儿~
耶瑟儿~ 2021-01-25 03:32

I need to display following special characters in a table TD (exclude the back slash in between < symbol and forward slash),

1) \'/<\\/@/tes

相关标签:
2条回答
  • 2021-01-25 04:03

    Everthing which get sent by JSP is by default treated as HTML by the webbrowser. The < indicates start of a HTML tag and thus the webbrowser will parse it as such (and eventually fail due to a syntax error).

    You want to escape those HTML special characters like <, >, & and ".

    &lt; will be displayed as <
    &gt; will be displayed as >
    &amp; will be displayed as &
    &quot; will be displayed as "
    

    If it is dynamic text, this is best to be done with JSTL <c:out> tag.

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

    Of if you want to set a HTML attribute, the JSTL fn:escapeXml() function is nicer.

    <%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    ...
    <input type="text" value="${fn:escapeXml(bean.text)}" />
    
    0 讨论(0)
  • 2021-01-25 04:03

    I got it to work on JSP by adding this directive to the page

    <%@ page contentType="text/html; charset=UTF-8" %>
    
    0 讨论(0)
提交回复
热议问题