• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

User specific logic within init method

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()).
 
Sheriff
Posts: 28372
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Space pants. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic