import java.io.*;
import javax.servlet.*;
import java.util.*;  //因为用到枚举
import javax.servlet.http.*;
public class SnoopServlet extends HttpServlet
{  
  public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
  { //发送的请求信息不能超过8k
    if(req.getContentLength()>=8*1024)
     {
       res.setContentType("text/html");
       ServletOutputStream out=res.getOutputStream();
       out.println("<html><head><title>too big</title></head>");
       out.println("<body><h1>error-content length &gt;8k not");
       out.println("</h1></body></html>");
     }
    else
     {  doGet(req,res);}
   }
  public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
   {
       res.setContentType("text/html");
       ServletOutputStream out=res.getOutputStream();
       out.println("<html><head><title>SnoopServlet</title></head>");

	//下面显示HttpServletRequest中包含的信息，有些从ServletRequest中继承
       out.println("<body><h2>request information</h2>");
	out.println("<pre>");
	out.print("<h3>");
	print(out, "request method:",req.getMethod());//调用重载的print()
	print(out,"request URI:",req.getRequestURI());
	print(out,"request protocol:",req.getProtocol());
	print(out,"servlet path:",req.getServletPath());//返回服务器方处理请求的Servlet路径
	print(out,"path info:",req.getPathInfo());//extra path information follows the servlet path but precedes the query string.
        print(out,"path translated:",req.getPathTranslated());//Returns any extra path information after the servlet name but before the query string, and translates it to a real path
	print(out,"Query String:",req.getQueryString());
	print(out,"content length:",req.getContentLength());//Returns the length, in bytes, of the request body and made available by the input stream, or -1 if the length is not known.
	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,"aythorization:",req.getAuthType());//Returns:one of the static members BASIC_AUTH, FORM_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH 
	out.println("</pre>");

	//下面显示请求的头部信息
	Enumeration 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>");
	   out.println("</h3>");
	}

	//下面显示Servlet参数
	e=req.getParameterNames();
	if(e.hasMoreElements())
	 {
   	   out.println("<h2>request parameters(single value)</h2>");
	   out.print("<h3>");
	   out.println("<pre>");
	   while(e.hasMoreElements())
		{
		String name=(String)e.nextElement();//因为返回对象类型
		out.println(" "+name+":"+req.getParameter(name));
		}
	   out.println("</pre>");
	   out.println("</h3>");
	}

	e=req.getParameterNames();
	if(e.hasMoreElements())
	 {
   	   out.println("<h2>request headers</h2>");
	   out.print("<h3>");
	   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>");
	}
       out.println("</body></html>");
    }

  //重载print()
  private void print(ServletOutputStream out,String name, String value) throws IOException
  {
	out.print(" "+name+":");
	out.println(value==null? "&lt;none&gt;":value);
  }

  private void print(ServletOutputStream out,String name, int value) throws IOException
  {
	out.print(" "+name+":");
	if(value==-1)
	out.println("&lt;none&gt;");
	else
	out.println(value);
  }
 }

 //可以输入：http://java.8080/servlet/SnoopServelt?name=djf&test=1