//计数
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class accessCounter extends HttpServlet
{ 
   int count;
   public void init(ServletConfig config) throws ServletException
    {
	super.init(config);
	try{
		FileReader fr=new FileReader("c:\\counter.initial");
		BufferedReader buf=new BufferedReader(fr);
		String initial=buf.readLine();
		count=Integer.parseInt(initial);
		return;
	}
	catch(FileNotFoundException e){count=0;}
	catch(IOException e){count=0;}
	catch(NumberFormatException e){count=0;}
    }//init()结束
//service()和doxxx()不能同时出现
  public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
    { //获得一个向客户发送数据的输出流：
       response.setContentType("text/html;charset=GBK");//设置响应的MIME类型。
       PrintWriter out=response.getWriter();
       out.println("<HTML> <BODY>");
        count++;                         //增加计数。
	out.println("<marquee>这个servlet以被访问"+count+"次</marquee>");
       out.println("</body> </html>");
       saveState();
    }//do over
	public void destory()
	{
		saveState();
	}

	public void saveState()
	{
		try{
			FileWriter fw=new FileWriter("c:\\counter.initial"); 
			String ini=Integer.toString(count);
			fw.write(ini,0,ini.length());
			fw.close();
		}
		catch(Exception e){}
	}
}