home *** CD-ROM | disk | FTP | other *** search
/ PC World 1999 November / PCWorld_1999-11_cd.bin / Komunik / Sambar / _setup.1 / Snoop.java < prev    next >
Text File  |  1999-08-28  |  4KB  |  130 lines

  1. package sysadmin;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import javax.servlet.*;
  6. import javax.servlet.http.*;
  7.  
  8.  
  9. /*
  10. ** Snoop servlet.
  11. */
  12. public
  13. class Snoop extends HttpServlet 
  14. {
  15.     public void init(ServletConfig config)
  16.         throws ServletException
  17.     {
  18.         super.init(config);
  19.     }
  20.  
  21.     public void destroy()
  22.     {
  23.         super.destroy();
  24.     }
  25.  
  26.     public String getServletInfo()
  27.     {
  28.         return    "Display servlet request headers.";
  29.     }
  30.  
  31.     public void service (HttpServletRequest req, HttpServletResponse res)
  32.     throws ServletException, IOException
  33.     {
  34.         // Only the Sambar System Administrator may run this servlet
  35.         String isAdmin = (String)req.getAttribute("isSambarAdmin");
  36.         if (isAdmin == null)
  37.             return;
  38.     
  39.         res.setContentType("text/html");
  40.         ServletOutputStream out = res.getOutputStream();
  41.         out.println("<html>");
  42.         out.println("<head><title>Snoop Servlet</title></head>");
  43.         out.println("<body>");
  44.  
  45.         out.println("<h1>Requested URL:</h1>");
  46.         out.println("<pre>");
  47.         out.println (HttpUtils.getRequestURL(req).toString());
  48.         out.println("</pre>");
  49.  
  50.         out.println("<h1>Request information:</h1>");
  51.         out.println("<pre>");
  52.         print(out, "Request method", req.getMethod());
  53.         print(out, "Request URI", req.getRequestURI());
  54.         print(out, "Request protocol", req.getProtocol());
  55.         print(out, "Servlet path", req.getServletPath());
  56.         print(out, "Path info", req.getPathInfo());
  57.         print(out, "Path translated", req.getPathTranslated());
  58.         print(out, "Query string", req.getQueryString());
  59.         print(out, "Content length", req.getContentLength());
  60.         print(out, "Content type", req.getContentType());
  61.         print(out, "Server name", req.getServerName());
  62.         print(out, "Server port", req.getServerPort());
  63.         print(out, "Remote user", req.getRemoteUser());
  64.         print(out, "Remote address", req.getRemoteAddr());
  65.         print(out, "Remote host", req.getRemoteHost());
  66.         out.println("</pre>");
  67.  
  68.         Enumeration e = req.getParameterNames();
  69.         if (e.hasMoreElements()) 
  70.         {
  71.             out.println("<h1>Servlet parameters (Single value):</h1>");
  72.             out.println("<pre>");
  73.             while (e.hasMoreElements()) 
  74.             {
  75.                 String name = (String)e.nextElement();
  76.                 out.println(" " + name + " = " + req.getParameter(name));
  77.             }
  78.             out.println("</pre>");
  79.         }
  80.  
  81.         e = req.getParameterNames();
  82.         if (e.hasMoreElements()) 
  83.         {
  84.             out.println("<h1>Servlet parameters (Multiple values):</h1>");
  85.             out.println("<pre>");
  86.             while (e.hasMoreElements()) 
  87.             {
  88.                 String name = (String)e.nextElement();
  89.                 String vals[] = (String []) req.getParameterValues(name);
  90.                 if (vals != null) 
  91.                 {
  92.                     out.print("<b> " + name + " = </b>"); 
  93.                     out.println(vals[0]);
  94.                     for (int i = 1; i<vals.length; i++)
  95.                         out.println("           " + vals[i]);
  96.                 }
  97.                 out.println("<p>");
  98.             }
  99.             out.println("</pre>");
  100.  
  101.             out.println("<h3>Cookies</h3>");
  102.             Cookie[] cookies =req.getCookies();
  103.             for (int i = 0; i < cookies.length; i++)
  104.             {
  105.                 out.println("<b>" + cookies[i].getName() + "</b> ");
  106.                 out.println("<i>" + cookies[i].getValue() + "</i><BR>");
  107.             }
  108.         }
  109.  
  110.         out.println("</body></html>");
  111.     }
  112.  
  113.     private void print(ServletOutputStream out, String name, String value)
  114.         throws IOException
  115.     {
  116.         out.print(" " + name + ": ");
  117.         out.println(value == null ? "<none>" : value);
  118.     }
  119.  
  120.     private void print(ServletOutputStream out, String name, int value)
  121.     throws IOException
  122.     {
  123.         out.print(" " + name + ": ");
  124.         if (value == -1)
  125.             out.println("<none>");
  126.         else
  127.             out.println(value);
  128.     }
  129. }
  130.