home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1999 November
/
PCWorld_1999-11_cd.bin
/
Komunik
/
Sambar
/
_setup.1
/
Snoop.java
< prev
next >
Wrap
Text File
|
1999-08-28
|
4KB
|
130 lines
package sysadmin;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/*
** Snoop servlet.
*/
public
class Snoop extends HttpServlet
{
public void init(ServletConfig config)
throws ServletException
{
super.init(config);
}
public void destroy()
{
super.destroy();
}
public String getServletInfo()
{
return "Display servlet request headers.";
}
public void service (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
// Only the Sambar System Administrator may run this servlet
String isAdmin = (String)req.getAttribute("isSambarAdmin");
if (isAdmin == null)
return;
res.setContentType("text/html");
ServletOutputStream out = res.getOutputStream();
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>");
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());
out.println("</pre>");
Enumeration e = req.getParameterNames();
if (e.hasMoreElements())
{
out.println("<h1>Servlet parameters (Single value):</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 values):</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>");
out.println("<h3>Cookies</h3>");
Cookie[] cookies =req.getCookies();
for (int i = 0; i < cookies.length; i++)
{
out.println("<b>" + cookies[i].getName() + "</b> ");
out.println("<i>" + cookies[i].getValue() + "</i><BR>");
}
}
out.println("</body></html>");
}
private void print(ServletOutputStream out, String name, String value)
throws IOException
{
out.print(" " + name + ": ");
out.println(value == null ? "<none>" : value);
}
private void print(ServletOutputStream out, String name, int value)
throws IOException
{
out.print(" " + name + ": ");
if (value == -1)
out.println("<none>");
else
out.println(value);
}
}