This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
You mean init() method is called everytime a request is received for servlet?
No, that is not what I mean. The init method is called when the servlet is 'put into service'. In other words, when the servlet is loaded into the container.
For the most part, this means that the init method will only be called once but this is not guaranteed by the spec.
Look at SRV.2.3.4:
SRV.2.3.4 End of Service The servlet container is not required to keep a servlet loaded for any particular period of time. A servlet instance may be kept active in a servlet container for a period of milliseconds, for the lifetime of the servlet container (which could be a number of days, months, or years), or any amount of time in between. When the servlet container determines that a servlet should be removed from service, it calls the destroy method of the Servlet interface to allow the servlet to release any resources it is using and save any persistent state. For example, the container may do this when it wants to conserve memory resources, or when it is being shut down. Before the servlet container calls the destroy method, it must allow any threads that are currently running in the service method of the servlet to complete execution, or exceed a server-defined time limit.
So, it's perfectly allowable, according to the spec, for a servlet to be taken out of service and then put back into service within the lifecycle of the the application. When being put back into service, I would expect the container to call the init method again.
Joined: Feb 19, 2007
Clap.... Clap.... clap....
Am Learning a lot in avery short period of time. And I will continue to do it...