5.3 Using Persistent Connections

Java Servlet Programming, 2nd Edition > 5. Sending HTML Information > 5.3 Using Persistent Connections

 
< BACKCONTINUE >

5.3 Using Persistent Connections

Persistent connections (sometimes called keep-alive connections ) can be used to optimize the way servlets return content to the client. To understand how this optimization works, you first need to understand how HTTP connections work. We'll keep this at a high level and go only as low as is necessary to explain the basic idea. The details are well covered in Clinton Wong's HTTP Pocket Reference (O'Reilly).

When a client, such as a browser, wants to request a web document from a server, it begins by establishing a socket connection to the server. Over this connection, the client makes its request and then receives the server's response. The client indicates it has finished its request by sending a blank line; the server, in turn, indicates that the response is complete by closing the socket connection.

So far, so good. But what if the retrieved page contains <IMG> tags or <APPLET> tags that require the client to retrieve more content from the server? Well, another socket connection is used. If a page contains 10 graphics along with an applet made up of 25 classes, that's 36 connections needed to transfer the page. No wonder some people say WWW stands for the World Wide Wait! This approach is like ordering a pizza, but making a separate phone call for each topping.

A better approach is to use the same socket connection to retrieve more than one piece of a page, something called a persistent connection. The trick with a persistent connection is that the client and server must somehow agree on where the server's response ends and where the client's next request begins. They could try to use a token like a blank line, but what if the response itself contains a blank line? The way persistent connections work is that the server just tells the client how big the response body will be by setting the Content-Length header as part of the response. The client then knows that after that much response body, it has control of the socket again.

Most servers internally manage the Content-Length header for the static files they serve; the Content-Length is set to match the file's length. To determine the content length of servlet-generated output, a server requires the servlet's assistance. A servlet can set the response Content-Length and gain the advantages of a persistent connection for its dynamic content by using the setContentLength( ) method:

public void ServletResponse.setContentLength(int len)

This method sets the length (in bytes) of the content being returned by the server. In an HTTP servlet, the method sets the HTTP Content-Length header. Note that using this method is optional. If you use it, however, your servlets will be able to take advantage of persistent connections when they are available. The client will also be able to display an accurate progress monitor during the download.

If you do call setContentLength( ), there are two caveats: a servlet must call this method before sending the response body, and the given length must be exact. If it's off by even one byte, you have the potential for problems.


Last updated on 3/20/2003
Java Servlet Programming, 2nd Edition, © 2001 O'Reilly

< BACKCONTINUE >


Java servlet programming
Java Servlet Programming (Java Series)
ISBN: 0596000405
EAN: 2147483647
Year: 2000
Pages: 223

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net