Java Reference
In-Depth Information
Java server faces and AJAX
JSF provides wrapper methods to simplify AJAX calls. If you're using JSF as your present-
ation technology, you'll most likely use the <f:ajax/> tag to make AJAX calls, which
invokes a method on a Java bean on the server. The JSF tag enables you to easily integ-
rate AJAX into your application without having to build the plumbing. In addition, the
JSF AJAX infrastructure will automatically update other JSF components, saving you the
trouble of writing code that iterates through the DOM.
The JavaScript code in this listing isn't very different than the WebSocket code in listing
14.3 . The major differences center on the functionality provided. AJAX uses a re-
quest-response model. There's no way to get server events back to the client asynchron-
ously without an initial request from the server. For example, if you're implementing chat
functionality, the client must repeatedly poll the server for chat messages if it's using
AJAX. This extra network traffic increases the load on the server while making the chat
service appear slow. AJAX is great for situations where the client needs to retrieve data
from the server for validation or partial page updates. It's not suitable in situations where
the server can generate data asynchronously or multiple messages need to be transmitted to
the server before a response is received.
Related to AJAX is Comet, which attempts to use AJAX to provide WebSocket-like func-
tionality prior to HTML5.
14.2.3. WebSockets versus Comet
The AJAX approach we discussed in the last section works perfectly for situations where
the client needs to request information from the server, but it's not a good solution for situ-
ations where the server needs to push events back to the client. The client can use AJAX
to poll the server, but this isn't an ideal solution for several reasons, including excess-
ive network traffic and latency. Comet isn't a well-defined standard but rather a technique
whereby existing web technologies are used to provide server-side push within the limita-
tions of HTTP 1.1. The limitations of HTTP 1.1, of course, are the lack of support for long-
lived connections whereby the server can push data back to the client. This is the problem
that WebSockets solves.
Search WWH ::




Custom Search