Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

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");
}
}

Log to Firefox console

You can log to the Firefox console by using the method "dump" in javascript. To enable this feature in Firefox type in about:config in the address bar and create there a new boolean entry with the name "browser.dom.window.dump.enabled" and the value "true".

Now start Firefox from the command line by entering "firefox -console". All messages passed to dump() will be printed on the command line.

To automatically add a timestamp to your messages and avoid error messages in other browsers I've created this little function:

function log(message) {
if(window.dump) {
var date = new Date();
var hours = date.getHours();
if(hours < 10) hours = "0" + hours;
var minutes = date.getMinutes();
if(minutes < 10) minutes = "0" + minutes;
var seconds = date.getSeconds();
if(seconds < 10) seconds = "0" + seconds;
dump(hours + ":" + minutes + ":" + seconds + " - someapp: " + message + "\n");
}
}

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