Ajax Communication: Introduction
Each time traditional web applications talk to the web server, they fetch a completely new HTML page. In contrast, AJAX applications offload the user interface logic to the client and make asynchronous calls to the server to send and receive only the data itself. This allows the UI of an AJAX application to be much more responsive and fluid while reducing an application’s bandwidth requirements and the load on the server.
At this point, you’ve created the initial implementation of the StockWatcher application, simulating stock data in the client-side code. In the following three tutorials, you will learn how to retrieve the stock data from a server.
Note: For a broader guide to client-server communication in a GWT application, see Communicate with a Server.
Choosing an Implementation Strategy
GWT provides a few different ways to communicate with a server. Which data format you use will ultimately depend on the server you need to interact with.
Making Remote Procedure Calls (GWT RPC)
If you can run Java on the backend and are creating an interface for your application’s server-side business logic, GWT RPC is probably your best choice. GWT RPC is a mechanism for passing Java objects to and from a server over standard HTTP. You can use the GWT RPC framework to transparently make calls to Java servlets and let GWT take care of low-level details like object serialization.
To try this out, see the tutorial, Making Remote Procedure Calls.
Retrieving JSON Data via HTTP
To try this out, see the tutorial, Retrieving JSON Data. Although this tutorial covers making HTTP requests only for JSON data, the code can be adapted to work with XML data instead.
Making Cross-Site Requests for JSONP
If you’re creating a mashup application that needs to use data from one or more remote web servers, you will have to work around SOP (Same Origin Policy) access restrictions. In this tutorial, you’ll use JsonpRequestBuilder to retrieve JSON with padding (JSONP).
To try this out, see the tutorial, Making Cross-Site Requests.
Making Asynchronous Calls
Whether you use GWT RPC or get JSON data via HTTP, all the calls you make from the HTML page to the server are asychronous. This means they do not block while waiting for the call to return. The code following the call executes immediately. When the call completes, the callback method you specified when you made the call will execute.
- You can perform other work while waiting on a pending server call. For example, you can build up your user interface while simultaneously retrieving the data from the server to populate the interface. This shortens the overall time it takes for the user to see the data on the page.
- You can make multiple server calls at the same time. However, just how much parallelism you can add using asynchronous calls is limited because browsers typically restrict the number of outgoing network connections to two at a time.
If you are new to AJAX development, the hardest thing to get used to about asynchronous calls is that the calls are non-blocking. However, Java inner classes go a long way toward making this manageable.
For more information on making asynchronous calls, see the Developer’s Guide, Getting Used to Asynchronous Calls