Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
2.6 The Servlet Life Cycle
35
depending on how you have registered the servlet with the Web server. It will
be created for the first user request if it is not explicitly registered but is
instead just placed in one of the standard server directories. See the discus
sion of Section 2.2 (A Simple Servlet Generating Plain Text) for details on
these directories.
There are two versions of
init
: one that takes no arguments and one that
takes a
ServletConfig
object as an argument. The first version is used when
the servlet does not need to read any settings that vary from server to server.
The method definition looks like this:
public void init() throws ServletException {
// Initialization code...
}
For examples of this type of initialization, see Section 2.8 (An Example
Using Servlet Initialization and Page Modification Dates) later in this chap
ter. Section 18.8 (Connection Pooling: A Case Study) in the chapter on JDBC
gives a more advanced application where
init
is used to preallocate multiple
database connections.
The second version of
init
is used when the servlet needs to read
server specific settings before it can complete the initialization. For example,
the servlet might need to know about database settings, password files,
server specific performance parameters, hit count files, or serialized cookie
data from previous requests. The second version of
init
looks like this:
public void init(ServletConfig config)
throws ServletException {
super.init(config);
// Initialization code...
}
Notice two things about this code. First, the
init
method takes a
Servlet
Config
as an argument.
ServletConfig
has a
getInitParameter
method
with which you can look up initialization parameters associated with the servlet.
Just as with the
getParameter
method used in the
init
method of applets,
both the input (the parameter name) and the output (the parameter value) are
strings. For a simple example of the use of initialization parameters, see Section
2.7 (An Example Using Initialization Parameters); for a more complex exam
ple, see Section 4.5 (Restricting Access to Web Pages) where the location of a
password file is given through the use of
getInitParameter
. Note that
although you look up parameters in a portable manner, you set them in a
server specific way. For example, with Tomcat, you embed servlet properties in
a file called
web.xml
, with the JSWDK you use
servlets.properties
, with
the WebLogic application server you use
weblogic.properties
, and with
the Java Web Server you set the properties interactively via the administration
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved