Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
2.7 An Example Using Initialization Parameters
39
in languages (such as ones named after letters of the alphabet) where it is
easy to read or write off the ends of arrays, make illegal typecasts, or have
dangling pointers due to memory reclamation errors. Besides, even Java
technology won't prevent someone from tripping over the power cable run
ning to the computer. So, don't count on
destroy
as the only mechanism for
saving state to disk. Activities like hit counting or accumulating lists of cookie
values that indicate special access should also proactively write their state to
disk periodically.
2.7 An Example Using
Initialization Parameters
Listing 2.8 shows a servlet that reads the
message
and
repeats
initialization
parameters when initialized. Figure 2 5 shows the result when
message
is
Shibboleth
,
repeats
is
5
, and the servlet is registered under the name
ShowMsg
. Remember that, although servlets read init parameters in a stan
dard way, developers set init parameters in a server specific manner. Please
refer to your server documentation for authoritative details. Listing 2.9 shows
the configuration file used with Tomcat to obtain the result of Figure 2 5,
Listing 2.10 shows the configuration file used with the JSWDK, and Figures
2 6 and 2 7 show how to set the parameters interactively with the Java Web
Server. The result is identical to Figure 2 5 in all three cases.
Because the process of setting init parameters is server specific, it is a good
idea to minimize the number of separate initialization entries that have to be
specified. This will limit the work you need to do when moving servlets that
use init parameters from one server to another. If you need to read a large
amount of data, I recommend that the init parameter itself merely give the
location of a parameter file, and that the real data go in that file. An example
of this approach is given in Section 4.5 (Restricting Access to Web Pages),
where the initialization parameter specifies nothing more than the location of
the password file.
Core Approach
For complex initializations, store the data in a separate file and use the init
parameters to give the location of that file.
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