Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
7.4 Using Persistent HTTP Connections
165
Listing 7.6 PersistentConnection.java
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
/** Illustrates the value of persistent HTTP connections for
* pages that include many images, applet classes, or
* other auxiliary content that would otherwise require
* a separate connection to retrieve.
*/
public class PersistentConnection extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
ByteArrayOutputStream byteStream =
new ByteArrayOutputStream(7000);
PrintWriter out = new PrintWriter(byteStream, true);
String persistenceFlag =
request.getParameter("usePersistence");
boolean usePersistence =
((persistenceFlag == null) ||
(!persistenceFlag.equals("no")));
String title;
if (usePersistence) {
title = "Using Persistent Connection";
} else {
title = "Not Using Persistent Connection";
}
out.println(ServletUtilities.headWithTitle(title) +
"\n" +
"" + title + "
");
int numImages = 100;
for(int i=0; i
out.println(makeImage(i, usePersistence));
}
out.println("