What is server push? How do I use it from a servlet?

Nicola Ken Barozzi

To understand what "server push" means you must remember that the web operates in an event-driven way; the events can come from clients or servers.

  • In the normal Web client-server scenario, the browser asks the server for the web pages. This is called "pull", because the client "pulls" the data from the server. The events, in this case, come from the client.
  • Sometimes it is necessary to have the server send data to the client; a typical case is that of stock data, which must change constantly on the web page to remain in sync with actual data, even if the client doesn't request it by clicking on a request button. This is called "push", because the server "pushes" the data to the client. The events, in this case, come from the server.

[However, even with so-called server-push, the server cannot actually initiate a TCP connection to the client. Instead, the server leaves the initial connection open; after the first chunk of data (a whole page, or a partial page, or a single image) is sent, the client is then waiting around for the server to send the next piece of information.

Note that this means that the server has to keep its sockets open, which can be expensive. Using an HTML Refresh (e.g. <META HTTP-EQUIV="Refresh" CONTENT="4;URL=http://www.example.com">) may be a better solution. -Alex]

On Javaworld you can find an explanation on how to implement server push with servlets in Pushlets, Part 1: Send events from servlets to DHTML client browsers. It describes of a pushlet framework.You may visit the pushlet Website through http://www.justobjects.nl/ to see how development is progressing, to run examples, and to download the framework source.