Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
164
Chapter 7 Generating the Server Response: HTTP Response Headers
that the document has finished loading by monitoring the number of bytes
received, and reconnects on the same socket for further transactions. Persis
tent connections of this type became standard in HTTP 1.1, and compliant
servers are supposed to use persistent connections unless the client explicitly
instructs them not to (either by a
Connection: close
request header or
indirectly by sending a request that specifies
HTTP/1.0
instead of
HTTP/1.1
and does not also stipulate
Connection: keep alive
).
Servlets can take advantage of persistent connections if the servlets are
embedded in servers that support them. The server should handle most of
the process, but it has no way to determine how large the returned document
is. So the servlet needs to set the
Content Length
response header by
means of
response.setContentLength
. A servlet can determine the size of
the returned document by buffering the output by means of a
ByteArray
OutputStream
, retrieving the number of bytes with the byte stream's
size
method, then sending the buffered output to the client by passing the serv
let's output stream to the byte stream's
writeTo
method.
Using persistent connections is likely to pay off only for servlets that load a
large number of small objects, where those objects are also servlet generated
and would thus not otherwise take advantage of the server's support for per
sistent connections. Even so, the advantage gained varies greatly from Web
server to Web server and even from Web browser to Web browser. For exam
ple, the default configuration for Sun's Java Web Server is to permit only five
connections on a single HTTP socket: a value that is too low for many appli
cations. Those who use this server can raise the limit by going to the adminis
tration console, selecting Web Service then Service Tuning, then entering
a value in the Connection Persistence window.
Listing 7.6 shows a servlet that generates a page with 100
IMG
tags (see
Figure 7 4 for the result). Each of the
IMG
tags refers to another servlet
(
ImageRetriever
, shown in Listing 7.7) that reads a GIF file from the server
system and returns it to the client. Both the original servlet and the
Image
Retriever
servlet use persistent connections unless instructed not to do so
by means of a parameter in the form data named
usePersistence
with a
value of
no
. With Netscape 4.7 and a 28.8K dialup connection to talk to the
Solaris version of Java Web Server 2.0 (with the connection limit raised above
100), the use of persistent connections reduced the average download time
between 15 and 20 percent.
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved