开发者

Display HTML encoded string in browser

开发者 https://www.devze.com 2023-02-27 11:29 出处:网络
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 encod

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 &gt; 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 di开发者_高级运维splay the string 4 &gt; 5 as is and not correctly decoded as 4 < 5?


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:

<?php
    header('Content-Type: text/plain');
    print $string;
?>

I don't know the Java syntax.

The <pre> tag doesn't work, no success with <code> either

<pre> just means white space is significant. <code> just means "This is an HTML representation of some code".


In this case, you'll actually need to represent the > as HTML entities. So, &amp;gt; should work I believe.

0

精彩评论

暂无评论...
验证码 换一张
取 消