Reputation: 1571
I have some HTML code that I store in a Java.lang.String variable. I write that variable to a file and set the encoding to UTF-8 when writing the contents of the string variable to the file on the filesystem. I open up that file and everything looks great e.g. → shows up as a right arrow.
However, if the same String (containing the same content) is used by a jsp page to render content in a browser, characters such as → show up as a question mark (?)
When storing content in the String variable, I make sure that I use:
String myStr = new String(bytes[], charset)
instead of just:
String myStr = "<html><head/><body>→</body></html>";
Can someone please tell me why the String content gets written to the filesystem perfectly but does not render in the jsp/browser?
Thanks.
Upvotes: 3
Views: 10135
Reputation: 5796
The lazy developer (=me) uses Apache Common Lang StringEscapeUtils.escapeHtml http://commons.apache.org/lang/api-release/org/apache/commons/lang/StringEscapeUtils.html#escapeHtml(java.lang.String) which will help you handle all 'odd' characters. Let the browser do the final translation of the html entities.
Upvotes: 0
Reputation: 1109635
but does not render in the jsp/browser?
You need to set the response encoding as well. In a JSP you can do this using
<%@ page pageEncoding="UTF-8" %>
This has actually the same effect as setting the following meta tag in HTML <head>
:
<meta http-equiv="content-type" content="text/html; charset=utf-8">
Upvotes: 4
Reputation: 1844
Possibilities:
Content-Type: text/html; charset=utf-8
in your HTTP Headers.Upvotes: 1