This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes User specific logic within init method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "User specific logic within init method" Watch "User specific logic within init method" New topic
Author

User specific logic within init method

Tim Hagberg
Greenhorn

Joined: Jan 29, 2010
Posts: 12
I've just discovered some user specific logic occurring within the init methods of several servlets. This has me a little confused as this logic executes every time someone performs a certain action, but I thought the init method was only called during the initialization of a servlet. My first assumption was that these servlets were being destroyed after every use, and thus the init method was being called every time one of the respective actions was performed, however I can't find any evidence of destroy methods being called. Has anyone seen anything like this, and are there easy ways to find out if a servlet is being destroyed before the container is shut down?
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

Welcome to JavaRanch!

Yes, the init() method should only be called once by the Container when the servlet is first created. Maybe the method is being called elsewhere in the code (which shouldn't be done, since only the Container should call init()).


SCJP 6 || SCWCD 5
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18708
    
    8

I don't know about "easy" ways; my servlet container writes entries to the log each time it initializes or destroys a servlet. Perhaps yours does too, or maybe it could be told to do that.
Parthiban Malayandi
Ranch Hand

Joined: Apr 15, 2008
Posts: 70
Tim Hagberg wrote: My first assumption was that these servlets were being destroyed after every use, and thus the init method was being called every time one of the respective actions was performed, however I can't find any evidence of destroy methods being called. Has anyone seen anything like this, and are there easy ways to find out if a servlet is being destroyed before the container is shut down?


Hi Tim,

We have init and destroy methods which serves the purpose but it is invoked whenever a servlet instance is created (Only once if it's the FIRST Request since at that time we won't be having any instance for THAT servlet ) and destroyed .NOT for every servlet Request.

For every request the SAME servlet instance is used with a NEW Thread.

As same with init, destroy method will also be called only if don't have any request for that particular Servlet INSTANCE

Following is the extract form API
==========================
Destroy

Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed. After the servlet container calls this method, it will not call the service method again on this servlet.

This method gives the servlet an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the servlet's current state in memory.

Hope this will clear your doubt.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: User specific logic within init method