Servlet Objects
98
Destruction of Servlets
The destruction of a servlet can come about as a result of two situations. The first situation is where a
servlet handles a requests and generates a response, whether that be successful or otherwise. The sec-
ond situation is where the HTTP client closes the connection before the servlet has sent a complete
response.
The fact that the actions of a servlet may need to be aborted before it has finished complicate the de-
struction of a servlet. This is because any callback which may have been set up will result in a reference
count against the servlet object. The existance of such references will actually prevent the immediate
destruction of the servlet object. If that reference is never deleted, the servlet object may never be de-
stroyed.
All this means that it isn’t sufficient for the servlet framework to delete its own reference to an instance
of a HTTP servlet and expect that it will be destroyed. Instead, it is necessary to introduce a special
member function to the HttpServlet class and require that any derived class extend it as appropri-
ate to cancel any callbacks or otherwise cause external or circular references to the servlet to be delet-
ed.
The name of this member function is "destroyServlet()". The member function will be called
when a HTTP client prematurely closes the connection. So that only one mechanism is employed to
ensure a servlet is destroyed, the member function is also called subsequent to a servlet generating a
complete response.
class HttpServlet(netsvc.HttpServlet,netsvc.Agent):
def __init__(self,session):
netsvc.HttpServlet.__init__(self,session)
netsvc.Agent.__init__(self)
def processRequest(self):
if self.requestMethod() != "GET":
self.sendError(400)
else:
self.sendResponse(200)
self.sendHeader("Content-Type","text/plain")
self.endHeaders()
self.startTimer(self.completeResponse,10,"timeout")
def completeResponse(self,tag):
self.sendContent("Hi there.")
self.endContent()
def destroyServlet(self):
netsvc.HttpServlet.destroyServlet(self)
netsvc.Agent.destroyReferences(self)
The first action of the derived version of the member function "destroyServlet()" should be to
call the base class version of the function in the HttpServlet class. The member function should
then do what is ever necessary to ensure that references to the servlet are deleted. If the servlet had