Listing 3: Servlet.java
import netscape.server.applet.HttpApplet;
import netscape.server.applet.URIUtil;
import java.io.PrintStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
class Servlet extends HttpApplet {
PrintStream outputStream;
Hashtable variables;
public String getValue(String name) {
if (variables != null) {
Enumeration enum = getValues(name);
if (enum.hasMoreElements()) {
return (String) enum.nextElement();
}
}
return "";
}
protected Enumeration getVariables() {
return variables.keys();
}
public Enumeration getValues(String name) {
Object o = variables.get(name);
if ((o != null) && (o instanceof Vector)) {
return ((Vector)o).elements();
}
Vector v = new Vector();
if (o != null) {
v.addElement(o);
}
return v.elements();
}
public void run() throws Exception {
returnNormalResponse("text/html");
outputStream = getOutputStream();
outputStream.println("<HTML>");
variables = new Hashtable();
try {
if (getMethod().equals("GET")) {
if (getQuery() != null)
variables = URIUtil.splitFormData(getQuery());
} else
variables = getFormData();
printBody();
}
catch (Exception e) {
outputStream.println("error: " + e);
}
outputStream.println("</HTML>");
}
protected void printBody() {
outputStream.println("<HEAD><TITLE>Variables</TITLE></HEAD>");
outputStream.println("<H2>Variables</H2><P>");
outputStream.println("<PRE>");
Enumeration e = getVariables();
while(e.hasMoreElements()) {
String key = (String) e.nextElement();
Enumeration valuesEnum = getValues(key);
while (valuesEnum.hasMoreElements()) {
outputStream.println(key + " = " + valuesEnum.nextElement());
}
}
outputStream.println("</PRE>");
outputStream.println("</BODY>");
}
}
// End of File
|