在浏览器中显示 HTML 编码的字符串
我目前正在尝试使用不同的 HTML 编码器来对我的 Java Web 应用程序中用户提供的值进行编码。我编写了一个小型示例应用程序,将不同编码器的结果打印到网站上。到目前为止,这没有任何问题。
不幸的是,浏览器(FireFox)也按预期运行,以正确的方式显示编码字符(例如将 >
转换为 <
)。在这种特殊情况下,我不希望发生这种情况,我想按原样查看编码字符串。我希望浏览器以与 Web 服务器发送字符串相同的方式显示字符串。
标记不起作用,
也没有成功。是否有一个我忽略的 HTML 标签来实现这一点?或者我可以使用其他技巧吗?我不想在服务器端以任何方式使用附加编码来操作字符串,以避免误导结果。
长话短说 - 如何让浏览器显示字符串 4 > 5
按原样,未正确解码为 4
5
?
I am currently experimenting with different HTML-encoders to encode user supplied values in my Java web application. I wrote a small sample application that prints the results from the different encoders to a website. This works so far without any issues.
Unfortunately the browser (FireFox) also behaves as expected, displaying the encoded characters in the correct way (e.g. transforms >
into <
). In this special case I do not want this to happen, I want to see the encoded string as it is. I want the browser to display the strings the same way the web server sends them.
The <pre>
tag doesn't work, no success with <code>
either. Is there a HTML-tag I have overlooked to accomplish that? Or is there another trick I can user? I do not want to manipulate the string in any way on the server side with additional encodings, to avoid misleading results.
To make a long question short - how do I get my browser to display the string 4 > 5
as is and not correctly decoded as 4 < 5
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不希望浏览器将文档视为 HTML,则不要将其作为 HTML 提供。
在 PHP 中你会这样做:
我不知道 Java 语法。
If you don't want the browser to treat the document as HTML, then don't serve it as HTML.
In PHP you would do:
I don't know the Java syntax.
<pre>
just means white space is significant.<code>
just means "This is an HTML representation of some code".在这种情况下,您实际上需要代表 >作为 HTML 实体。所以,我相信
>
应该有效。In this case, you'll actually need to represent the > as HTML entities. So,
>
should work I believe.