Java 如何在 JSP 中转义特殊的 HTML 字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/475839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:07:25  来源:igfitidea点击:

How can I escape special HTML characters in JSP?

javahtmljspescaping

提问by Free Wildebeest

Before I go and create a custom tag or Java method to do it, what is the standard way to escape HTML characters in JSP?

在我创建自定义标记或 Java 方法之前,在 JSP 中转义 HTML 字符的标准方法是什么?

I have a String object and I want to display it in the HTML so that it appears to the user as is.

我有一个 String 对象,我想在 HTML 中显示它,以便它按原样显示给用户。

For example:

例如:

String a = "Hello < World";

Would become:

会成为:

Hello &lt; World

采纳答案by Slartibartfast

Short answer:

简短的回答:

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

there is another option:

还有另一种选择:

<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
${fn:escapeXml(myString)}