Explain the life cycle methods of a servlet ?
The Web container is responsible for managing the servlet’s life cycle. The following figure is explaining the life cycle of a Servlet.
The Web container creates an instance of the Servlet and then the container calls the
init() method. At the completion of the
init() method the Servlet is in ready state to service requests from clients. The container calls the servlet’s
service() method for handling each request by spawning a new thread / by picking up an available thread for each request from the Web container’s thread pool . * * Once the response is sent back to the client and the connection between Web-Browser and the Web-Server is closed, the dedicated thread is recycled back to the pool and is ready to serve other tasks.
Before destroying the instance the container will call the
destroy() method. After destroy() the Servlet becomes the potential candidate for garbage collection.
* * HTTP 1.0 to HTTP 1.1
A major improvement in the HTTP 1.1 standard is persistent connections. In HTTP 1.0, a connection between a Web client and server is closed after a single request/response cycle. In HTTP 1.1, a connection is kept alive and reused for multiple requests. Persistent connections reduce communication lag perceptibly, because the client doesn't need to renegotiate the TCP connection after each request.