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("");
    if (usePersistence) {
      response.setContentLength(byteStream.size());
    }
    byteStream.writeTo(response.getOutputStream());
  }
  private String makeImage(int n, boolean usePersistence) {
    String file =
      "/servlet/coreservlets.ImageRetriever?gifLocation=" +
      "/bullets/bullet" + n + ".gif";
    if (!usePersistence)
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




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

jsp web hosting

 

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