how to display a text with tags in jsp

梦想的初衷 提交于 2019-12-01 17:55:56

问题


I want to display a text called

"welcome<to>Jsp"

In page source also i'm seeing as "welcomeJsp"

But in HTML its displaying as "welcomeJsp" alone. Please guide me.


回答1:


You have to escape these characters..

welcome&lt;to&gt;Jsp

I would advice using Apache's StringEscapeUtils class(available in org.apache.commons.lang ) function escapeHtml() for escaping HTML.

StringEscapeUtils.escapeHtml("welcome<to>Jsp")




回答2:


"welcome&lt;to&gt;Jsp"


&gt; (greater than) - (>)
&lt; (less than)    - (<)

These characters need to be encoded this way to actually display them.




回答3:


Use JSTL <c:out> tag or fn:escapeXml() function.

<c:out value="welcome<to>Jsp" />

or

${fn:escapeXml('welcome<to>Jsp')}

You can even use it on model values.

<c:out value="${bean.property}" />

or

${fn:escapeXml(bean.property)}

Those two should by the way always be used when it concerns user-controlled input, you're otherwise totally open to XSS attack holes. See also our JSP wiki page and What is the general concept behind XSS?



来源:https://stackoverflow.com/questions/13303128/how-to-display-a-text-with-tags-in-jsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!