home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 November / PCONLINE_11_99.ISO / filesbbs / OS2 / APCHSSL2.ZIP / OS2HTTPD / servlets / KeepAliveServlet.java < prev    next >
Encoding:
Java Source  |  1999-02-10  |  4.9 KB  |  139 lines

  1. /*
  2.  * @(#)KeepAliveServlet.java    1.3 97/05/22
  3.  * 
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.0
  20.  */
  21.  
  22. import java.io.*;
  23.  
  24. import javax.servlet.*;
  25. import javax.servlet.http.*;
  26.  
  27. /**
  28.  * This is an example of different ways to do keepalive connections
  29.  * with servlets. The Content-Length header needs to be set for
  30.  * keep-alive to work.  This is done automatically for servlets with
  31.  * small amounts of output (<4K), but needs to be done manually
  32.  * otherwise.
  33.  * <P> 
  34.  * <B>What is keep-alive, and why should you care?</B><BR>
  35.  * <P>
  36.  * When a browser talks to a server, it needs to establish a TCP connection.
  37.  * Establishing this connection is expensive in terms of time.  Keep-Alive
  38.  * connections are connections which keep the connection open for subsequent
  39.  * requests, greatly speeding the downloading of data from the server.
  40.  * By ensuring that you follow the simple rules illustrated below, you
  41.  * can enable automatic establishment of Keep-Alive connections from your
  42.  * servlet.
  43.  *
  44.  * @author Jim Driscoll
  45.  * @version 1.3 05/22/97
  46.  *
  47.  */
  48.  
  49. public class KeepAliveServlet extends HttpServlet { 
  50.  
  51.     public void doGet(HttpServletRequest req, HttpServletResponse res)
  52.     throws ServletException, IOException {
  53.  
  54.         res.setContentType("text/html");
  55.         ServletOutputStream out = res.getOutputStream();
  56.  
  57.     //Set up the strings we will use for output
  58.     String str1 = "<HTML><HEAD><TITLE> Keep-Alive Servlet Output";
  59.     str1 = str1 + "</TITLE></HEAD><BODY>\r\n";
  60.     String str2 = "<h1> Keep-Alive Servlet Output </h1>\r\n";
  61.     String str3 = "<P>This is output from Keep-Alive Servlet.\r\n";
  62.     String str4 = "</BODY></HTML>\r\n";
  63.     int strlen1 = str1.length();
  64.     int strlen2 = str2.length();
  65.     int strlen3 = str3.length();
  66.     int strlen4 = str4.length();
  67.  
  68.     String runType = req.getParameter("runType");
  69.     if (runType == null ||runType.equals("short nohead")) {;
  70.         // First the "normal" way - keepalive and content-length should
  71.         // be automatically set
  72.             out.print(str1);
  73.         out.print(str2);
  74.         out.print(str3);
  75.         out.print(str4);
  76.         out.close();
  77.         } else if (runType.equals("short nohead flush")) {
  78.         // If you have a flush() of the output stream,
  79.         // content length can't be determined - so,
  80.         // keepalive will not be enabled!  (Don't worry,
  81.             // the output stream is flushed on close - you
  82.         // won't lose any data)
  83.             out.print(str1);
  84.         out.print(str2);
  85.         out.print(str3);
  86.         out.flush();
  87.         out.print(str4);
  88.         out.close();
  89.         } else if (runType.equals("long nohead")) {
  90.             // This way tests a large body with no content-length specified
  91.         // It will not, normally, have keepalive enabled
  92.         // Note that the cut-off for automatic determination of
  93.         // content-length is 4k bytes in JavaWebServer
  94.             out.print(str1);
  95.         out.print(str2);
  96.         for (int i = 0; i < 100;i++) {
  97.         out.println(str3);
  98.         }
  99.         out.print(str4);
  100.         out.close();
  101.     } else if (runType.equals("short head")) {
  102.         // This way manually sets the content-length,
  103.         // keepalive should be on automatically
  104.         res.setContentLength(strlen1+strlen2+strlen3+strlen4);
  105.             out.print(str1);
  106.         out.print(str2);
  107.         out.print(str3);
  108.         out.print(str4);
  109.         out.close();
  110.     } else if (runType.equals("long head")) {
  111.         // This way manually sets the content-length on long output,
  112.         // which should cause keepalive to be enabled
  113.         res.setContentLength(strlen1+strlen2+(strlen3*100)+strlen4);
  114.             out.print(str1);
  115.         out.print(str2);
  116.         for (int i = 0; i < 100;i++) {
  117.         out.print(str3);
  118.         }
  119.         out.print(str4);
  120.         out.close();
  121.     } else { //Bogus value
  122.             out.println("<HTML><HEAD><TITLE> Keep-Alive Servlet Output </TITLE></HEAD><BODY>");
  123.         out.println("<h1> Keep-Alive Servlet Output </h1>");
  124.         out.println("<P>Bogus value set for querystring.");
  125.         out.println("<P>Set the runType parameter to one of:.");
  126.         out.println("short+nohead");
  127.         out.println("short+head");
  128.         out.println("long+nohead");
  129.         out.println("long+head");
  130.         out.println("</BODY></HTML>");
  131.         out.close();
  132.     }
  133.     }
  134.  
  135.     public String getServletInfo() {
  136.         return "A servlet which demonstrates keep-alive programming";
  137.     }
  138. }
  139.