There is a lot of AJAX chats popping out, the reason: it is very simple to automate communication between client (AJAX part) and with java, it is easy to distribute messages between clients in server-side. I will show you the implementation used in SMS chat’s.
There are several niceties that everyone includes such as enter key interception and refocusing on the chat entry field, for examples of either, just look at the source attached. The server side in Java is just as easy.
Since servlets are created once per application, you can use init method to instantiate a chat holding String or a Buffer (or any other self created object). Since this object will be automatically shared per any request to the servlet, you just add messages to it and post back to each requestor. Synchronization can be implemented as and if needed.