import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class statusServlet extends HttpServlet
{
  public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
   {
     String addr=req.getRemoteAddr();
     if("127.0.0.1".equals(addr))
	doResponse(res,"you are authorized to view this page.");
     else
	{
	StringBuffer sb=new StringBuffer();
 	sb.append(getServletContext().getRealPath("/"));//servlet在文件系统中的绝对路径
	sb.append("bad\\"); //添加bad子目录
	sb.append("ip");  //创建文件名，以ip打头
	sb.append(addr.replace('.','_'));   //去文件名的后面部分
	sb.append(".log");   //为文件名加扩展名
	synchronized(this){  //需同步
	  try{
	   PrintWriter out=new PrintWriter(new FileWriter(sb.toString(),true));//表示添加而不是覆盖
	   out.println(" ");
	   out.println(new Date());
	   out.println("ACCESS ATTEMPT FROM INVALID REMOTE ADDRESS.");
	   out.println("remote address;"+addr);
	   out.println("remote host:"+req.getRemoteHost());
	   out.println("query string:"+req.getQueryString());
	   out.close();
	    }catch(Exception e){}
	}
	res.sendRedirect("http://localhost/servlet/accessDenied");
	}
}

public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
 {
   doGet(req,res);
 }

public void doResponse(HttpServletResponse res, String str) throws IOException
 {
    res.setContentType("text/html");
    PrintWriter out=res.getWriter();
    out.println("<html><head><title>CGI environment variables</title></head>");
    out.println("<body BGCOLOR=\"#FFFFFFF\">");
    out.println(str);
    out.println("</body></html>");
    out.close();
 }
}

//没建拒绝页时
//定位在http://localhost/servlet/statusServlet
//在定位在http://localhost/servlet/statusServlet?name=admin&password=admin
//几次运行结束后看.log文件