load order on servlets

A

Andy Fish

Hi,

I have two servlets in the same web app. For historical reasons, servlet A
does much of the initialisation, so must be initialised before servlet B can
run.

Given that they are in the same web app, am I guaranteed that they will both
be initialized before either is called?

if not, is there any way to programmatically initialize one from inside the
other, or to specify this in my web.xml? I know some servlet containers
support forced loading, but I want to assume as little as possible about the
configuration of the container.

I realise the best way is to abstract out all the common initialzation code
and call it from either, but I really don't want to open up a big can of
worms

many thanks

Andy
 
C

Chris Riesbeck

Andy Fish said:
Hi,

I have two servlets in the same web app. For historical reasons, servlet A
does much of the initialisation, so must be initialised before servlet B can
run.

Given that they are in the same web app, am I guaranteed that they will both
be initialized before either is called?

if not, is there any way to programmatically initialize one from inside the
other, or to specify this in my web.xml?

http://www.jguru.com/faq/view.jsp?EID=135396
 
J

John C. Bollinger

This design is flawed. If servlet A is performing initialization tasks
that servlet B later uses then servlet A is initializing the web
application. That is not the role of a servlet, and it is likely to
cause you trouble. The way to initialize a web application is to
install one or more ServletContextListeners in it.

I wish this annoying bit of misinformation would disappear. The article
is correct, as far as it goes, but (1) performing initialization via
load-on-startup is the wrong thing to do (see above), and (2) it may
open you up to unpleasant surprises if you don't understand exactly what
load-on-startup promises and what it doesn't. In particular, the
servlet spec nowhere guarantees how long a servlet container will retain
a servlet instance that is not in use. The container will only use one
instance of each servlet at a time, but it is free to discard one and
start using a different one almost at will. And if it does, then it
will init() the new instance, by the way, which often causes problems if
init() is responsible for initializing more than just one servlet instance.
Ideally I would like to get my servlets to load on demand with a dependency,
so if there's an easy way to do that I'd still be interested to hear about
it. However, this solution is "fit for purpose"

Initialize your webapp with the tools designed for that purpose. Let
your servlets focus on servicing requests.



John Bollinger
(e-mail address removed)
 
A

Andy Fish

The way to initialize a web application is to
install one or more ServletContextListeners in it.
Initialize your webapp with the tools designed for that purpose. Let
your servlets focus on servicing requests.

John,

Thanks for this idea. I have read up on ServletContextListener and can see
how it would be used to perform initialization.

However, I don't get the impression that this is what it is designed for, or
even that this is normal practice - I would have expected everyone to be
shouting loud and clear "this is the correct way to initialize a web app",
but everything I've read seems to mention it in passing as just an
incidental interface.

None of the documentation I've seen about servlet.init() makes the point
about what to do if you have multiple servlets accessing the same underlying
code in a web app. Documentation about designing a web app (as opposed to a
single servlet) seems rather thin on the ground.

Anyway, I definitely have a better idea how to go about initialization next
time. In the meantime, unfortunately I have a nightmare deadline to meet for
integrating two legacy servlet apps together, so the load-on-startup
solution seems to be the one I'm stuck with

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top