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

  1. /*
  2.  * @(#)EchoServlet.java    1.5 97/11/13
  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. import java.util.*;
  24.  
  25. import javax.servlet.*;
  26. import javax.servlet.http.*;
  27.  
  28.  
  29. /**
  30.  * Echo servlet.  A variation on Snoop Servlet.  This servlet will echo the input content
  31.  * body back out with no processing.
  32.  * @version     1.5, 11/13/97
  33.  * @author     Jim Driscoll
  34.  */
  35.  
  36. public class EchoServlet extends HttpServlet {
  37.  
  38.  
  39.     // Since entity bodies are only present in POST, we'll just use that.
  40.     public void doPost (HttpServletRequest req, HttpServletResponse res)
  41.     throws ServletException, IOException
  42.     {
  43.  
  44.  
  45.     int buffsize = 1024*8;
  46.  
  47.         //value chosen to limit denial of service - 1 MB, in this case
  48.         if (req.getContentLength() > 1*1024*1024) {  
  49.         res.setContentType("text/html");
  50.             ServletOutputStream out = res.getOutputStream();
  51.         out.println("<html><head><title>Too big</title></head>");
  52.         out.println("<body><h1>Error - content length >8k not ");
  53.         out.println("</h1></body></html>");
  54.         } else {
  55.         res.setContentType("text/html");
  56.         ServletOutputStream out = res.getOutputStream();
  57.         out.println("<html>");
  58.         out.println("<head><title>Echo Servlet</title></head>");
  59.         out.println("<body>");
  60.     
  61.         Enumeration e = req.getHeaderNames();
  62.         if (e.hasMoreElements()) {
  63.             out.println("<h1>Request headers:</h1>");
  64.             out.println("<pre>");
  65.             while (e.hasMoreElements()) {
  66.             String name = (String)e.nextElement();
  67.             out.println(" " + name + ": " + req.getHeader(name));
  68.             }
  69.             out.println("</pre>");
  70.         }
  71.     
  72.         out.flush();
  73.  
  74.             out.println("<h1>Content: </h1>");
  75.         out.println("<pre>");
  76.         ServletInputStream in = req.getInputStream();
  77.  
  78.         int contentLength = req.getContentLength();
  79.             byte[] b = new byte[contentLength];
  80.         int result;
  81.         int totalRead = 0;
  82.  
  83.         try {
  84.         result = in.readLine(b, 0, b.length);
  85.         while (result != -1) {
  86.  
  87.             totalRead += result;
  88.             result = in.readLine(b, totalRead, buffsize);
  89.         }
  90.         } catch (IOException ioe) {
  91.         // Ignore, it's probably from EOF
  92.         }
  93.  
  94.         out.write(b, 0, totalRead);
  95.         out.flush();
  96.             
  97.         out.println("</pre>");
  98.     
  99.         out.println("</body></html>");
  100.         out.flush();
  101.         }
  102.     }
  103.     
  104.     public String getServletInfo() {
  105.     return "A servlet that shows the request headers and body sent by the client";
  106.     }
  107. }
  108.