FileDocCategorySizeDatePackage
EchoFormServlet.javaAPI DocExample2206Thu Apr 03 15:28:44 BST 1997None

EchoFormServlet

public class EchoFormServlet extends HttpServlet

Fields Summary
Constructors Summary
Methods Summary
public voidservice(HttpServletRequest request, HttpServletResponse response)


    String msg = "<HTML><HEAD><TITLE>\r\n";
    
    msg += "Form Response</TITLE></HEAD><BODY><H1>Form Response</H1>\r\n";
    
    // get the request 
    msg += "Method: " + request.getMethod() + "<br>\r\n";
    msg += "URI: " + request.getRequestURI() + "<br>\r\n";
    msg += "Protocol: " + request.getProtocol() + "<br>\r\n";
    msg += "Path Info: " + request.getPathInfo() + "<br>\r\n";

    // get MIME header name-value pairs
    Enumeration e = request.getHeaderNames();
    while (e.hasMoreElements()) {
      String header = (String) e.nextElement();
      msg += header + ": " + request.getHeader(header) + "<br>\r\n";
    }
    msg += "<P>";

    // read the form data
    String qs = "";
    if (request.getMethod().equals("GET")) {
      qs = request.getQueryString();
    }
    else if (request.getMethod().equals("POST")) {
      try {
        DataInputStream dis = new DataInputStream(request.getInputStream());
        String s;
        while ((s = dis.readLine()) != null) {
          qs += s + "\r\n";
        }
      }
      catch (IOException ie) {
      }
    }
    else {
      msg += "I can't handle the " + request.getMethod() + " method.<br>";
    }
    
    // split the form data into pieces
    // For the moment no URL decoding is done
    if (qs != null) {
      StringTokenizer st = new StringTokenizer(qs, "&");
      while (st.hasMoreTokens()) {
        String temp = st.nextToken();
        String name = temp.substring(0, temp.indexOf('="));
        String value = temp.substring(temp.indexOf('=") + 1, temp.length());
        msg += name + ": " + value + "<br>\r\n";
      } 
    }
    
    // Finish the HTML
    msg += "</BODY></HTML>\r\n";

    // send the output to the client
    response.setContentLength(msg.length());
    response.setContentType("text/html");
    try {
      DataOutputStream rs = new DataOutputStream(response.getOutputStream());
      rs.writeBytes("\r\n" + msg);
    }
    catch (IOException ie) {
    }