Previous Page Next Page Start of Tutorial > Start of Trail > Start of Lesson Lesson Contents

Trail:
Servlets
Lesson:
The Life Cycle of a Servlet



Help on searching
is available.

Search technology
by Excite, Inc.

Initializing a Servlet

The init method provided by the HttpServlet class initializes the servlet and logs the initialization. To do initialization specific to your servlet, override the init method following these rules:
 

  • If an initialization error occurs that renders the servlet incapable of handling client requests, throw an UnavailableException.

    An example of this type of error is the inability to establish a required network connection.
     

  • Do not call the System.exit method
     
  • Save the ServletConfig parameter so that the getServletConfig method can return the value.

    The simplest way to do this is to have the new init method call super.init. If you store the object yourself, override the getServletConfig method to return the object from its new location.
     

Here is an example init method:

    public class BookDBServlet ... {

        private BookstoreDB books;

        public void init(ServletConfig config) throws ServletException {

            // Store the ServletConfig object and log the initialization
            super.init(config);

            // Load the database to prepare for requests
            books = new BookstoreDB();
        }
        ...
    }

The init method is quite simple: it calls the super.init method to manage the ServletConfig object and log the initialization, and sets a private field.

If the BookDBServlet used an actual database, instead of simulating one with an object, the init method would be more complex. Here is pseudo-code for what the init method might look like:

    public class BookDBServlet ... {

        public void init(ServletConfig config) throws ServletException {

            // Store the ServletConfig object and log the initialization
            super.init(config);

            // Open a database connection to prepare for requests
            try {
                databaseUrl = getInitParameter("databaseUrl");
                ... // get user and password parameters the same way
                connection = DriverManager.getConnection(databaseUrl,
                                                         user, password);
            } catch(Exception e) {
                throw new UnavailableException (this,
		    "Could not open a connection to the database");
            }
        }
        ...
    }

 

Initialization Parameters

The second version of the init method calls the getInitParameter method. This method takes the parameter name as an argument and returns a String representation of the parameter's value.

(The specification of initialization parameters is server-specific. For example, the parameters are specified with a property when a servlet is run with the servlet runner. The servletrunner Utility lesson contains a general explanation of properties and how to create them.)

If, for some reason, you need to get the parameter names, use the getParameterNames method.

 
Previous Page Next Page Start of Tutorial > Start of Trail > Start of Lesson Lesson Contents