Search code examples
javaencodingutf8-decodewindows-1255

Java string encoding conversion within a webpage


I have a webpage that is encoded (through its header) as WIN-1255. A Java program creates text string that are automatically embedded in the page. The problem is that the original strings are encoded in UTF-8, thus creating a Gibberish text field in the page.

Unfortunately, I can not change the page encoding - it's required by a customer propriety system.

Any ideas?

UPDATE:

The page I'm creating is an RSS feed that needs to be set to WIN-1255, showing information taken from another feed that is encoded in UTF-8.

SECOND UPDATE:

Thanks for all the responses. I've managed to convert th string, and yet, Gibberish. Problem was that XML encoding should be set in addition to the header encoding.

Adam


Solution

  • To the point, you need to set the encoding of the response writer. With only a response header you're basically only instructing the client application which encoding to use to interpret/display the page. This ain't going to work if the response itself is written with a different encoding.

    The context where you have this problem is entirely unclear (please elaborate about it as well in future problems like this), so here are several solutions:

    If it is JSP, you need to set the following in top of JSP to set the response encoding:

    <%@ page pageEncoding="WIN-1255" %>
    

    If it is Servlet, you need to set the following before any first flush to set the response encoding:

    response.setCharacterEncoding("WIN-1255");
    

    Both by the way automagically implicitly set the Content-Type response header with a charset parameter to instruct the client to use the same encoding to interpret/display the page. Also see this article for more information.

    If it is a homegrown application which relies on the basic java.net and/or java.io API's, then you need to write the characters through an OutputStreamWriter which is constructed using the constructor taking 2 arguments wherein you can specify the encoding:

    Writer writer = new OutputStreamWriter(someOutputStream, "WIN-1255");