• Post Reply Bookmark Topic Watch Topic
  • New Topic

ServletContextListener firing more then once  RSS feed

 
Kris Reid
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I going? Or isn't the "contextInitialized" method of the ServletContextListener only ment to happen once?

I have the following servlet



Which prints when I start the server:

Loading 1
init 1
unload 0
Loading 1
Loading 1
init 1
init 1

How is this possible? Shouldn't there be only one servlet? And why would it unload?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is this servlet installed in the servlet container - ie what does the web.xml for the web application look like?
Are there other servlets in this application?
From reading the JavaDocs, I gather that your implementation of ServletContextListener in an actual servlet is not what the designers had in mind.
Bill
 
Manesh Kumar
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also print the object instance in both init and contextInitialized method.

System.out.println("Instance = " + this);
 
Kris Reid
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point that the listener probably shouldn't be a actual Servlet.

But anyway shouldn't it still only load once?? I have created a test class and it does the same thing




Output:

Test Loading 1
Test unLoading 0
Test Loading 1
Test Loading 1
Test unLoading 0
Test unLoading 0


All I have in the web.xml for this new class is



This is also running on Sun App Server
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!