I'm writting a servlet filter that manipulates the http response body
(injecting HTML). It works fine with pages using the English charset,
but when processing a page with double-byte chars, some of the
characters are junk.
When processing the OutputStream, I create a ByteArrayOutputStream
baStream = new ByteArrayOutputStream();
then I create a string (forcing it to UTF-8) with that stream:
String str = new String(baStream.toByteArray(), "UTF-8");
I then manipulate that string using standard regex, then output it back
to the browser:
outStream.write(str.getBytes());
The problem is I don't know a lot about how charsets work in Java. I
do know that Java's native string charset is UTF-16, but beyond that,
I'm not sure how to make sure that what comes into my servlet filter is
what goes out.
Thanks in advance!
Rich
Lothar Kimmeringer - 23 Sep 2006 10:07 GMT
> outStream.write(str.getBytes());
here you should use str.getBytes("UTF-8");
Alternatively use a Writer instead of an OutputStream, that
you can get from the servlet as well. Then you can write
String direclty without coping with the encoding to be used.
Or you wrap an OutputStreamWriter around your OutputStream
with specifying the encoding you want to use within the
constructor.
Regards, Lothar

Signature
Lothar Kimmeringer E-Mail: spamfang@kimmeringer.de
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
Always remember: The answer is forty-two, there can only be wrong
questions!