import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class CountInitDestroy extends HttpServlet {
	int count;
	public void init(){
	try {
			FileReader fr = new FileReader("CountInitDestroy.begining");
			BufferedReader br = new BufferedReader(fr);
			String begining = br.readLine();
			count = Integer.parseInt(begining);
			return;
			
		} catch (FileNotFoundException f) {
		} 
		catch (IOException io) {
		}
		catch (NumberFormatException nf) {
		} 
		String beginning = getInitParameter("beginning");
		try {
			count = Integer.parseInt(beginning);
			return;
		} catch (NumberFormatException ignored) {
		} 
		count = 0;
	}

	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		res.setContentType("text/plain");
		PrintWriter pw = res.getWriter();
		count++;
		if(count==1)
		pw.println("From the beginning, this servlet has been accessed "
				+ count + " time.");
		else
		{
			pw.println("From the beginning, this servlet has been accessed "
					+ count + " times.");
		}
	}

	public void destroy() {
		state();
	}

	public void state() {
		try {
			FileWriter fileWriter = new FileWriter("CountInitDestroy.beginning");
			String begin = Integer.toString(count);
			fileWriter.write(begin, 0, begin.length());
			fileWriter.close();
			return;
			
		} catch (IOException e) {
		}
	}
}