File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How to find if  the servlet is newly created or it was already existing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to find if  the servlet is newly created or it was already existing" Watch "How to find if  the servlet is newly created or it was already existing" New topic
Author

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

Pankaj Tiwari
Ranch Hand

Joined: Sep 11, 2005
Posts: 55
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

Joined: Dec 28, 2006
Posts: 893
When a servlet is newly created it's init() method will be called


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Pankaj Tiwari
Ranch Hand

Joined: Sep 11, 2005
Posts: 55
thats what i told..but he did not appeared to be very satisfied with answer [ ]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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?


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Rohit Suman
Ranch Hand

Joined: Sep 09, 2006
Posts: 37
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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Oct 11, 2004
Posts: 3830
I forgot the available listeners. Can any listener do the trick???
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Have you looked?
Pankaj Tiwari
Ranch Hand

Joined: Sep 11, 2005
Posts: 55
ContextListeners is what i can think of but it is for an application. Called everytime application is reloaded.
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to find if the servlet is newly created or it was already existing
 
Similar Threads
How to invalidate user's session forcefully
How do I bind existing Listener to Queue which is newly created by runtime?
DWR - How to refer object already exist in session
Trying to add Object values to a List, but the last value keeps getting added for all the values
problem with jsp:useBean