How Servlet Works

A)When user clicks on link that has a URL of Servlet.
Client Browse URL

B) Web Container(Tomcat) receives request for servlet , so it create two objects :
1)HttpServletRequest.
2)HttpServletResponse.

HTTPServlet


C)Container finds correct servlet on the basis of URL passed with the help deployment descriptor (web.xml) file. Creates / Allocate thread for that request and pass request and response object to servle thread.

Create Thread


D)Web Container calls the service() method of Servlet , service calls doGet() or doPost() methods on the type of request.

Service Method


E)service() calls the doPost() method or doPost() method which generates dynamic page then add the page in response object.

Servlet DoPost


F)When Thread completes, container converts the response object into HttpResponse object and destroys the response and request object.

Destroy Response