为什么 Java 字符串中的撇号在 HTML 中显示为“'”?
为什么 Java 字符串:
"God's wrath"
在 HTML 中显示为“
God's wrath
如何避免这种情况?”
Why does the Java string:
"God's wrath"
appear in HTML as
God's wrath
How to avoid this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果生成的字符串在用
'
引用的 HTML 属性中使用,则会对该字符进行转义以防止 XSS。请参阅OWASP XSS 预防备忘单。我认为禁用此类安全预防措施不是一个好主意,因为它们不会损害正常行为。
This character is escaped to prevent XSS if the resulting string is used in HTML attribute quoted with
'
. See OWASP XSS Prevention Cheat Sheet.I don't think it's a good idea to disable such security precautions, since they don't harm the normal behaviour.
就像“<”、“>”和其他字符一样,引号会被该代码替换,以避免浏览器呈现页面时出现令人讨厌的意外情况。请查看 W3Schools 提供的此列表,了解代码及其各自符号的完整列表。
Just like the '<', '>', and other characters, the quote is replaced by that code to avoid nasty surprises when the browser is rendering the page. Take a look at this list by W3Schools for a complete list of codes and their respective symbol.
如果您使用 JSTL,则 c:out 标记具有 escapeXml 属性,您可以将其设置为 false 以避免对字符进行编码。
If you're using JSTL the c:out tag has an escapeXml attribute, you can set it to false to avoid encoding characters.
特殊字符显示为 & 转义数字,因为它们在 HTML 中具有某种含义。在本例中,' 用于启动字符串文字。如果您希望它实际显示 ' 那么您必须将其替换为转义。这就是原因。
我不知道你为什么要避免它。
Special characters appear as ampersand escaped numbers because they mean something in HTML. In this case the ' is used to start a string literal. if you want it to actually show the ' then you have to replace it with the escaping. That's the why.
I don't know why you'd want to avoid it though.