Servlet Tutorials

How a Servlet Application works
Previous Home Next
adplus-dvertising

Servlet Application working as follows

In Servlet Application is provides a web container for responsible for managing execution of servlets and JSP pages for Java EE application.

It performed a request comes in for a servlet,and server hands the request to the Web Container.

In web Container is responsible for instantiating the servlet or creating a new thread to handle the request and it gives a job of Web Container to get the request and response to the servlet. The container creates multiple threads to process multiple requests to a single servlet.

In this application to get a Servlets don't have a main() method. Web Container manages the life cycle of a Servlet instance.

How a Servlet works
  1. In Servlet to get a user sends request for a servlet by clicking a link that has URL to a servlet.
  2. When the container finds the servlet using deployment descriptor and creates two objects :
    1. HttpServletRequest
    2. HttpServletResponse
  3. After that the container creates or allocates a thread for that request and calls the Servlet's service() method and passes the request, response objects as arguments.
  4. call to service() method with request and response object
  5. The service() method, then decides which servlet method, doGet() or doPost() to call, based on HTTP Request Method(Get, Post etc) sent by the client. Suppose the client sent an HTTP GET request, so the service() will call Servlet's doGet() method.
  6. call to doGet() or doPost() in Servlet execution
  7. Then the Servlet uses response object to write the response back to the client.
  8. Sending back the response to the client after servlet execution
  9. After the service() method is completed the thread dies. And the request and response objects are ready for garbage collection.
  10. End of Servlet Execution
Previous Home Next