FileDocCategorySizeDatePackage
LifeCycleServlet.javaAPI DocExample2086Wed Apr 05 11:25:42 BST 2000None

LifeCycleServlet

public class LifeCycleServlet extends HttpServlet

Fields Summary
int
timesAccessed
Constructors Summary
Methods Summary
public voiddestroy()


    // Write the Integer to a file
    File r = new File("./data/counter.dat");
    try {
      DataOutputStream dout = new DataOutputStream(new FileOutputStream(r));
      dout.writeInt(timesAccessed);
      dout.close();
    } 
    catch(IOException e) {
      // This should be logged 
    }

  
public voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)

    
    resp.setContentType("text/html");
    PrintWriter out = resp.getWriter();
  
    timesAccessed++;
   
    out.println("<HTML>");
    out.println("<HEAD>");
    out.println("<TITLE>Life Cycle Servlet</TITLE>");
    out.println("</HEAD><BODY>");
     
    out.println("I have been accessed " + timesAccessed + " time[s]");
    out.println("</BODY></HTML>"); 
  
public voidinit(javax.servlet.ServletConfig conf)


    super.init(conf);

    // Get initial value
    try {
      timesAccessed = Integer.parseInt(getInitParameter("defaultStart"));
    } 
    catch(NullPointerException e) {
     timesAccessed = 0;
    } 
    catch(NumberFormatException e) {
      timesAccessed = 0;
    }

    // Try loading from the disk
    try {
      File r = new File("./data/counter.dat");      
      DataInputStream ds = new DataInputStream(new FileInputStream(r));
      timesAccessed = ds.readInt();
      ds.close();
    } 
    catch (FileNotFoundException e) {
      // Handle error
    } 
    catch (IOException e) {
      // This should be logged
    }