AJAX, Unicode and Java

As you might already noticed I'm actually busy in using JavaScript. That's mainly because I develop an AJAX application based on J2EE. To simplify my work with JavaScript I use the library mootools which is a great facilitation over plain JavaScript, especially if you'd like to to your stuff object oriented.

By using the library I came around a strange problem with unicode handling. My application was written completely in UTF-8 but as soon as I sent data over AJAX to the server it didn't get handled correctly.

After some search I came across the solution: simply add the following line of to the top of your Servlet to let it know the POST data is UTF-8 encoded.

public class ChatServlet extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet {

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
}
}

0 comments:

Post a Comment

Copyright © 2008 - J2EE on Geronimo - is proudly powered by Blogger
Smashing Magazine - Design Disease - Blog and Web - Dilectio Blogger Template