How can I unload a servlet in its init() method? Can I unload it in the servlet constructor?
How can I unload a servlet from its container using the servlet's constructor or init() method?
If I unload it using the s开发者_如何学Pythonervlet constructor, will the init() method still be called?
Should I throw an exception? If so, checked or runtime?Just throwing an exception in the servlet's constructor or init()
method will prevent it from being taken in the servlet mapping of the servletcontainer.
You can not unload it at a later time when it has already been constructed and initialized successfully. Best what you can do is to just throw an exception in any of the HTTP methods based on some condition.
The right approach however depends on the sole functional requirement. Most probably you do not need a servlet at all. Simply because the desire to unload it manually makes design technically no utter sense.
if we call destroy() on servlet then it doesn't mean that our servlet will be unloaded/destroyed. It simply calls destroy leaving servlet untouched, nothing harm to servlet instance. It is still alive because you called destroy method. It is not container mechanism which is calling destroy method.
when container decides to destroy/unload the servlet instance from memory then container runs destruction mechanism and destroy method is one(along with several steps) of steps of the destruction mechanism. Destruction mechanism give chance to user/developer to clean-up resources which were initialized during the construction/initializing of the instance.
精彩评论