Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

Need help with String encoding issue

Thread view: 
rich.manalang@gmail.com - 22 Sep 2006 23:16 GMT
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!



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.