JAVAGeek JAVAGeek - 9 months ago 70
Java Question

How to set character encoding in servlet

I am sending plain text to a servlet using jquery ajax POST.
In my servlet i am setting the request character encoding like this :


and after that i am saving that text in database.

Now in another servlet i want to response the text stored in DB ...
All the things are working fine.i can see the text on browser window when i call servlet without any problem..

but when i save that file and try to open it in notepad(windows) , i got some characters missing and displayed as a small rectangular box. however the text file looks fine in other editors like
edit+ and notepad++

I tried this with no success..

response.setContentType("text/plain; charset=utf-8");

what is wrong in this ?

Answer Source

If it looks fine in other editors, then the text itself is fine. If it looks OK in the browser, then the response is probably fine too (but better check page info in the browser and see what the encoding is). Your problem is probably with notepad itself. Sometimes it requires BOM to detect Unicode properly. But BOM can break other apps that don't support it. You should also try Notepad on different versions of Windows. I have just tried opening an UTF-8 file in Windows 7, looks fine to me.