|
|
|
|||
|
Initializing a ServletThe
Here is an example
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 If the BookDBServlet used an actual database, instead of simulating one
with an object, the
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 ParametersThe second version of the (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 If, for some reason, you need to get the parameter names, use the
| |||
|