import java.servlet.*; import java.servlet.http.*; import java.io.*; import java.util.*; public class EchoFormServlet extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) { String msg = "\r\n"; msg += "Form Response

Form Response

\r\n"; // get the request msg += "Method: " + request.getMethod() + "
\r\n"; msg += "URI: " + request.getRequestURI() + "
\r\n"; msg += "Protocol: " + request.getProtocol() + "
\r\n"; msg += "Path Info: " + request.getPathInfo() + "
\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) + "
\r\n"; } msg += "

"; // 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.
"; } // 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 + "
\r\n"; } } // Finish the HTML msg += "\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) { } } }