Chapter 2. JavaScript for Ajax

Ajax is centered around the clever use of JavaScript. It isn’t a web framework, like Struts or Tapestry, and it isn’t some fancy new technology with a cool acronym; Ajax boils down to using JavaScript to interact directly with the web server, avoiding the submit/response cycle all too familiar to web users.

Java programmers have typically avoided JavaScript, sometimes for good reasons and sometimes for bad ones. Certainly, adding another layer of scripting to a JSP page can only add to the confusion. However, JavaScript runs entirely on the browser and is therefore very fast. There’s no waiting for the server to generate a response: JavaScript can compute a result and update the page immediately.

Ajax adds server interaction, but without the Submit button. Whenever data is needed, the JavaScript in the web page makes a request, and the server replies with data—but not another HTML page. The server returns data that the JavaScript displays in the existing page. The result is that your web application feels a lot more like a desktop application. In short, you can achieve a rich application experience in your web pages by using Ajax.

This book won’t attempt to teach JavaScript, or even to analyze its pros and cons. I assume that you have had some exposure to JavaScript. If you’re new to it, check out JavaScript: The Definitive Guide, by David Flanagan (O’Reilly). This is the best JavaScript reference available. JavaScript isn’t Java, though reading JavaScript ...

Get Ajax on Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.