Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to find if the servlet is newly created or it was already existing

 
Pankaj Tiwari
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".

Thanks and Regards
Pankaj Tiwari
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a servlet is newly created it's init() method will be called
 
Pankaj Tiwari
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thats what i told..but he did not appeared to be very satisfied with answer [ ]
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rohit Suman
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot the available listeners. Can any listener do the trick???
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked?
 
Pankaj Tiwari
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ContextListeners is what i can think of but it is for an application. Called everytime application is reloaded.
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know for sure if in the standard spec there is defined a listener for the event life cycle of servlets.

On the net I found that IBM has implemented a listener for servlets


Interface ServletListener
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic