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 = "
";
// 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) {
}
}
}