Book Home Java Enterprise in a Nutshell Search this book

5.4. Custom Servlet Initialization

At the beginning of this chapter, I talked about how a servlet's persistence can be used to build more efficient web applications. This is accomplished via class variables and the init() method. When a server loads a servlet for the first time, it calls the servlet's init() method and does not make any service calls until init() has finished. In the default implementation, init() simply handles some basic housekeeping, but a servlet can override the method to perform whatever one-time tasks are required. This often means doing some sort of I/O-intensive resource creation, such as opening a database connection. You can also use the init() method to create threads that perform various ongoing tasks. For instance, a servlet that monitors the status of machines on a network might create a separate thread to periodically ping each machine. When an actual request occurs, the service methods in the servlet can use the resources created in init(). Thus, the status monitor servlet might display an HTML table with the status of the various machines. The default init() implementation is not a do-nothing method, so you should remember to always call the super.init() method as the first action in your own init() routines.[5]

[5] Note that you no longer have to do this with Version 2.1 of the Servlet API. The specification has been changed so that you can simply override a no-argument init() method, which is called by the Generic Servletinit(ServletConfig) implementation.

The server passes the init() method a ServletConfig object, which can include specific servlet configuration parameters (for instance, the list of machines to monitor). ServletConfig encapsulates the servlet initialization parameters, which are accessed via the getInitParameter() and getInitParameterNames() methods. GenericServlet and HttpServlet both implement the ServletConfig interface, so these methods are always available in a servlet (one of the things the default init() implementation does is store the ServletConfig object for these methods, which is why it is important that you always call super.init()). Different web servers have different ways of setting initialization parameters, so we aren't going to discuss how to set them. Consult your server documentation for details.

Every servlet also has a destroy() method that can be overwritten. This method is called when, for whatever reason, a server unloads a servlet. You can use this method to ensure that important resources are freed, or that threads are allowed to finish executing unmolested. Unlike init(), the default implementation of destroy() is a do-nothing method, so you don't have to worry about invoking the superclass' destroy() method.

Example 5-4 shows a counter servlet that saves its state between server shutdowns. It uses the init() method to first try to load a default value from a servlet initialization parameter. Next the init() method tries to open a file named /data/counter.dat and read an integer from it. When the servlet is shut down, the destroy() method creates a new counter.dat file with the current hit-count for the servlet.

Example 5-4. A Persistent Counter Servlet

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

public class LifeCycleServlet extends HttpServlet {

  int timesAccessed;

  public void init(ServletConfig conf) throws ServletException {

    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();
    } 
    catch (FileNotFoundException e) {
      // Handle error
    } 
    catch (IOException e) {
      // This should be logged
    }    
    finally {
      ds.close();
    }
  }
        
  public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    
    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 void destroy() {

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


Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.