java servlets init and destroy
servlets uses init() to initialize servlets status and destroy 开发者_StackOverflowto clean up. Is there a special class name we need to put init() and destroy()? How does servlets know where to find these methods?
asp.net has a global.asax to handle similar thing in asp.net , servlets has a special class to do the same thing?
Thanks
Servlets will ultimately be a subclass of the javax.servlet.Servlet
class, which defines those methods.
It should be in the same class as your Servlet class.
All servlets inherit this method from the base Servlet class. Unless you want to do some additional processing, the inherited method should be fine and you dont need to override this method in each of your servlets.
Servlets are deployed in the Container(Web Server/Application Server),That container will take care of initializing or destroying of servlets,and we dont have predefined classes to initialise and destroy for servlets,if we use particular class that means we are depending on that class(Tightly Coupling) thats not recomended. Now we are using GenericServlet,and HttpServlet classes for that methods..once refer J2EE API and find this classes and methods in them..
The JVM which runs the Servlet, looks for those methods only in classes, which extend Servlet
or HttpServlet
.
Servlets are managed objects. This means that they are executed inside a container that manages their lifecycles (instantiates the servlets, call their relevant methods when it is appropiate, and releases them). The container (Tomcat, Glassfish, ...) knows when to call these methods at the right time because it is implemented that way, there is nothing special about that.
If the container had a bug, it could even call, say, destroy() at init time and init() at destroy time. That bug would be fixed quickly, though.
精彩评论