12 October 2010
0 Request Handling using Servlet
Request Handling using Servlet
The servlet can handle the request only after proper initialization. The request handling phase is carried out by the service method of the servlet life cycle. The service method of the Servlet takes ServletRequest and ServletRespose as the parameters. HTTPRequest object holds information about the request, client and the form data. Servlet then create a response to the request by using the provided object of type ServletResponse. For a HTTP request, the request and response objects are of type HttpServletRequest and HttpServletResponse respectively.
Most of the containers will create only one instance of a Servlet. The same instance is used for servicing all the client request. For each client request, the container executes the service method in a thread. So for multiple client requests, only multiple threads are created and not multiple objects. This model requires lesser memory and lesser cpu time compared to the multiple object model.
The Servlet need not to be alive throughout. It can be active in a container for milliseconds or as long as the container is up or any amount of time in between. Before unloading the servlet from the memory it may be required to release the resource or save the persistence state. To do this the servlet container provides the destroy method which is called by the server before removing the servlet from the memory. But before calling the servlet destroy() method servlet container waits for the threads that are running in the service to either complete, or exceed a server defined time limit. Once the destroy is called no further requests can be processed by that instance of the servlet. In order to process new requests servlet container needs to create a new instance of the servlet's class. Once the destroy method finishes, the servlet container releases the servlet. And is collected by the garbage collector.
Subscribe to:
Post Comments (Atom)
0 comments:
Please give your valuable comments.