• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

init method called twice!

 
Joshua Antony
Ranch Hand
Posts: 254
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a servlet called as HelloWorld and a filter corresponding to that servlet called as HelloWorldFilter.

I have specified in the deployment descriptor.

I have init method in both the servlet and filter with println statements in the method.

I am using Tomcat as web server and web container.
When I start Tomcat, to my surprise the init methods of both the servlet and filter are called twice.

Below is part of the output in the console

HelloWorldFilter : init method
HelloWorld constructor :
HelloWorldServlet : init() method

Is servlet config null ? yes
inside init() method
Is servlet config null ? no
context initializes
attribute added:
HelloWorldFilter : init method
HelloWorld constructor :
HelloWorldServlet : init() method


Shouldn't the init methods of both the filter and the servlet be called only once ?
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure that that's what's happening here, but the init and destroy methods of both servlets and filters can be called any number of times. The only guarantee is that each of those will only be called once for any given instance of those objects. If the container decides to take a particular servlet instance out of service (for whatever reason), then the next call to that servlet will cause another servlet instance to be created (and its init method called). Same for filters.

So you shouldn't rely on those methods being called exactly once overall.
 
Joshua Antony
Ranch Hand
Posts: 254
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf,

If the container decides to take a particular servlet instance out of service (for whatever reason), then the next call to that servlet will cause another servlet instance to be created (and its init method called). Same for filters.


But in my case, I don't think there is any multiple calling to the servlet going on.
I get those output when I restart the server, and I guess the container should create only 1 instance of the servlet and the filer unless I have used single threaded model(which I havent used at all).

I remember reading from K&B that there will only be 1 instance of servlet created and future request to the servlet will be executed in a new thread.

Please correct me if I am wrong.

Regards,
Joshua.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But in my case, I don't think there is any multiple calling to the servlet going on.

Not multiple callings, but multiple servlet creations and GCs.

I remember reading from K&B that there will only be 1 instance of servlet created and future request to the servlet will be executed in a new thread.

In general, the container will create only a single instance. But it is free to get rid of the instance it has, and create a new one, if for some reason it feels like doing so.

I agree that this should not be happening generally, and it may not be what's going on here. I just wanted to point out that it's perfectly legal for the container to create multiple servlet instances in this fashion.
 
Joshua Antony
Ranch Hand
Posts: 254
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree that this should not be happening generally, and it may not be what's going on here. I just wanted to point out that it's perfectly legal for the container to create multiple servlet instances in this fashion.


Ok, got the point

Is there any setting in Tomcat through which we can specify that the container create only 1 instance of servlets ?

Regards,
Joshua
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any setting in Tomcat through which we can specify that the container create only 1 instance of servlets?
Why would you want to do this? The container (in this case, Tomcat) will create a number of servlets it feels is appropriate to manage the number of requests it receives; it is the case that some containers create a pool of servlet instances to spread the load of handling requests, but more commonly they use only one servlet instance carefully. Even so, the entire point of using a container structure like J2EE is so you don't have to worry about such issues, so why do you want to be able to change it?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic