File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes init method called twice! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "init method called twice!" Watch "init method called twice!" New topic
Author

init method called twice!

Joshua Antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 254
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 ?


SCJP,SCWCD, Into ATG now!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42267
    
  64
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.


Ping & DNS - my free Android networking tools app
Joshua Antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 254
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
Marshal

Joined: Mar 22, 2005
Posts: 42267
    
  64
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

Joined: Jun 05, 2006
Posts: 254
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

Joined: Mar 27, 2003
Posts: 836
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?


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: init method called twice!