- Published on
Snoop servlet
- Authors
- Name
- Arnaud Ferrand
- @arferrand
My first ever exectued servlet from Sun's example back in 1999.
/*
* @(#)SnoopServlet.java 1.4 99/01/27
*
* Copyright (c) 1995-1998 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Sun.
*
* 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.
*
* CopyrightVersion 1.0
*/
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.cert.X509Certificate;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpUtils;
/**
* Snoop servlet. This servlet simply echos back the request line and headers
* that were sent by the client, plus any HTTPS information which is accessible.
*
* @version 1.4
* @author Various
*/
public class SnoopServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter out;
res.setContentType("text/html");
out = res.getWriter();
out.println("<html>");
out.println("<head><title>Snoop Servlet</title></head>");
out.println("<body>");
out.println("<h1>Requested URL:</h1>");
out.println("<pre>");
out.println(HttpUtils.getRequestURL(req).toString());
out.println("</pre>");
Enumeration e = getServletConfig().getInitParameterNames();
if (e != null) {
boolean first = true;
while (e.hasMoreElements()) {
if (first) {
out.println("<h1>Init Parameters</h1>");
out.println("<pre>");
first = false;
}
String param = (String) e.nextElement();
out.println(" " + param + ": " + getInitParameter(param));
}
out.println("</pre>");
}
out.println("<h1>Request information:</h1>");
out.println("<pre>");
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("</pre>");
e = req.getHeaderNames();
if (e.hasMoreElements()) {
out.println("<h1>Request headers:</h1>");
out.println("<pre>");
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
out.println(" " + name + ": " + req.getHeader(name));
}
out.println("</pre>");
}
e = req.getParameterNames();
if (e.hasMoreElements()) {
out.println("<h1>Servlet parameters (Single Value style):</h1>");
out.println("<pre>");
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
out.println(" " + name + " = " + req.getParameter(name));
}
out.println("</pre>");
}
e = req.getParameterNames();
if (e.hasMoreElements()) {
out.println("<h1>Servlet parameters (Multiple Value style):</h1>");
out.println("<pre>");
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
String vals[] = (String[]) req.getParameterValues(name);
if (vals != null) {
out.print("<b> " + name + " = </b>");
out.println(vals[0]);
for (int i = 1; i < vals.length; i++)
out.println(" " + vals[i]);
}
out.println("<p>");
}
out.println("</pre>");
}
String cipherSuite = (String) req
.getAttribute("javax.net.ssl.cipher_suite");
if (cipherSuite != null) {
X509Certificate certChain[] = (X509Certificate[]) req
.getAttribute("javax.net.ssl.peer_certificates");
out.println("<h1>HTTPS Information:</h1>");
out.println("<pre>");
out.println("Cipher Suite: " + cipherSuite);
if (certChain != null) {
for (int i = 0; i < certChain.length; i++) {
out.println("client cert chain [" + i + "] = "
+ certChain[i].toString());
}
}
out.println("</pre>");
}
out.println("<h1>Java Information:</h1>");
out.println("<pre>");
out.println("Java version: " + System.getProperty("java.version"));
out.println("Java home: " + System.getProperty("java.home"));
out.println("Java vendor: " + System.getProperty("java.vendor"));
out.println("Java class version: "
+ System.getProperty("java.class.version"));
out.println("Java class path:");
String cp = System.getProperty("java.class.path");
out.println(cp.replace(File.pathSeparatorChar, '\n'));
out.println("</pre>");
out.println("</body></html>");
}
private void print(PrintWriter out, String name, String value) {
out.print(" " + name + ": ");
out.println(value == null ? "<none>" : value);
}
private void print(PrintWriter out, String name, int value) {
out.print(" " + name + ": ");
if (value == -1) {
out.println("<none>");
} else {
out.println(value);
}
}
}
it outputs
http://localhost:8080/examples/snoop
Request information:
Request method: GET
Request URI: /examples/snoop
Request protocol: HTTP/1.1
Servlet path: /snoop
Path info: <none>
Path translated: <none>
Query string: <none>
Content length: <none>
Content type: <none>
Server name: localhost
Server port: 8080
Remote user: <none>
Remote address: 127.0.0.1
Remote host: 127.0.0.1
Authorization scheme: <none>
Request headers:
host: localhost:8080
user-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.6) Gecko/20061201 Firefox/2.0.0.6 (Ubuntu-feisty)
accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
accept-language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
accept-encoding: gzip,deflate
accept-charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
keep-alive: 300
connection: keep-alive
cookie: JSESSIONID=A9D649EA0D7259B4A788E1A4303703D8
Java Information:
Java version: 1.5.0_11
Java home: /usr/lib/jvm/java-1.5.0-sun-1.5.0.11/jre
Java vendor: Sun Microsystems Inc.
Java class version: 49.0
Java class path:
/opt/apache-tomcat-5.5.23/bin/bootstrap.jar
/usr/lib/jvm/java-1.5.0-sun-1.5.0.11/lib/tools.jar