Hello Friends,
This is the tutorial regarding java servlet container. This include all the information about how the Servlet Container works in the web server, how it gets the requests and response and how it handles the same and provides the output to be displayed.
Let’s consider the simple example, a client clicks the link on the browser and waits for the result to be displayed on the browser. Now, just when the client clicks the link the request is transferred from the browser to the server and server provide the response and displays the output on the screen, but in all of this there is no role of container mentioned. This is because client is not aware of and neither must he care for what happens at the back end, the only thing he cares is about the results he is showed on the browser. But we will think otherwise and get into it about what actually helps the browser to display the exact result.
As we are talking about the servlets, the web server comes into the picture. Now, when the browser requests for the servlet then the request is passed on to the container (tomcat server) first instead of sending it directly to the servlet. The servlet is already deployed in the container. It’s the container that handles the request from the browser and sends the response back to it.
Let’s have a look how the container handles the request.
1) At first, the client will click the link or URL on the browser and will wait for the response. This is nothing but the request to servlet page. Now, once the link is clicked , the browser will send the Http Request to the Container.
Once the request is with Container, it will create the two HTTP objects for HttpRequest and HttpResponse.
2) After this, the container will look into the requested URL and find out the appropriate Servlet. Then correspondingly, container allocates the thread to process this servlet request and passes the request and response object to it.
3) Once the thread is created, the container calls the Servlet’s service() method.
4) The service() method will call either the doGet() or doPost() method and will create the dynamic response page.
5) This dynamic response is sent back to the container along with the HttpResponse object.
6) Now, the container will generate the HttpResponse from the dynamic page and sent it back to the browser with the requested results.
7) Once the response is sent back to the browser, container deletes the request and response objects which it created earlier, and also deallocates or removes the thread which was allocated to the servlet.
In this way the container handles the requests and responses of the servlets.
Hope this will help you in understanding the container and its working.
Regards,
admin@code2java.com