/* * @(#)SnoopServlet.java 1.6 96/07/28 David Connelly * * Copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice * appears in all copies. Please refer to the file "copyright.html" * for further important copyright and licensing information. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ import java.io.*; import java.util.*; import java.servlet.*; /** * Snoop servlet. This servlet simply echos back the request line and * headers that were sent by the client. * * @version 1.6, 07/28/96 * @author David Connelly */ public class SnoopServlet extends Servlet { public void service(ServletRequest req, ServletResponse res) throws IOException { res.setContentType("text/html"); res.setHeader("Pragma", "no-cache"); res.writeHeaders(); PrintStream out = new PrintStream(res.getOutputStream()); out.println(""); out.println("
");
print(out, "Request method", req.getMethod());
print(out, "Request URI", req.getRequestURI());
print(out, "Request protocol", req.getProtocol());
print(out, "Servlet path", req.getServletPath());
print(out, "Path info", req.getPathInfo());
print(out, "Path translated", req.getPathTranslated());
print(out, "Query string", req.getQueryString());
print(out, "Content length", req.getContentLength());
print(out, "Content type", req.getContentType());
print(out, "Server name", req.getServerName());
print(out, "Server port", req.getServerPort());
print(out, "Remote user", req.getRemoteUser());
print(out, "Remote address", req.getRemoteAddr());
print(out, "Remote host", req.getRemoteHost());
print(out, "Authorization scheme", req.getAuthType());
out.println("");
/*
Hashtable params = req.getQueryParameters();
if (params.size() > 0) {
out.println("");
Enumeration e = params.keys();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
out.println(" " + name + " = " + (String)params.get(name));
}
out.println("");
}
*/
if (!req.getProtocol().equals("HTTP/0.9")) {
out.println("");
String name;
for (int i = 0; (name = req.getHeaderName(i)) != null; i++) {
out.println(" " + name + ": " + req.getHeader(name));
}
out.println("");
}
out.println("");
}
private void print(PrintStream out, String name, String value) {
out.print(" " + name + ": ");
out.println(value == null ? "<none>" : value);
}
private void print(PrintStream out, String name, int value) {
out.print(" " + name + ": ");
if (value == -1) {
out.println("<none>");
} else {
out.println(value);
}
}
private static final String UNKNOWN = "<unknown>";
public String getServletInfo() {
return "A servlet that shows the request headers sent by the client";
}
}