• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

User specific logic within init method

 
Tim Hagberg
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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?
 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • 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()).
 
Paul Clapham
Sheriff
Posts: 20983
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
Parthiban Malayandi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • 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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic