If you are allowed to use javascript library then pick one of the available libraries (jquery, protoype etc). I personally prefer jquery as it has great api. You can find many example for using jquery ajax with java. Have a look at Jquery with Java web app
In some scenarios where you want to alert user about data validation without submitting form, then you can use "false" as third parameter. Then you will receive reponse from server and you can display it to user.