O'Reilly logo

Head First Servlets and JSP, 2nd Edition by Bert Bates, Bryan Basham, Kathy Sierra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Servlets are controlled by the Container

In chapter two we looked at the Container’s overall role in a servlet’s life—it creates the request and response objects, creates or allocates a new thread for the servlet, and calls the servlet’s service() method, passing the request and response references as arguments. Here’s a quick review...

  1. image with no caption

    User clicks a link that has a URL to a servlet.

  2. image with no caption

    The Container “sees” that the request is for a servlet, so the container creates two objects:

    1) HttpServletResponse

    2) HttpServletRequest

  3. image with no caption

    The Container finds the correct servlet based on the URL in the request, creates or allocates a thread for that request, and calls the servlet’s service() method, passing the request and response objects as arguments.

  4. image with no caption

    The service() method figures out which servlet method to call based on the HTTP Method (GET, POST, etc.) sent by the client.

    The client sent an HTTP GET request, so the service() method calls the servlet’s doGet() method, passing the request and response objects as arguments.

  5. image with no caption

    The servlet uses the response object to write out the response to the client. The response goes back through the Container.

  6. image with no caption

    The service() method completes, so the thread either dies or returns to a Container-managed thread pool. The request and response object references fall out of scope, so these objects are toast (ready for garbage collection).

    The client gets the response.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required