Dear All I was ask this question at an interview. But i do not know whether it is possible or not. Can anyone help. The interviewer asked me "how will you find out that the servlet was already existing or is newly created".
I also would go with 'init()' methods invocation thats where we developers would get to know as and when a servlet is instantiated and invoked.
If at all, we can have a workaround by using some date/time variables being set as and when the servlet is invoked ,and whenever required you can retrieve that information.
I think this would be of some little help! Other ranchers any suggestions here?
putting some s.o.p line in init method would have notified in console that servlet is newly created else if request comes if sop line doesnot get printed then its an existing one
It is also possible to track the usage of the Class via either a static or instance block in the code or even the no-arg constructor, but as has been mentioned above, I'd still prefer to do it in the init() method.