How Container Handle Requests in Java

Introduction 

 
A Container is a Java application that controls a servlet. When a container starts, it looks for the deployed web applications and finds the servlet classes. After it finds the servlet class, it is up to the container to load the class immediately or at the time of client request. One thing that is ensured is that the servlet is initialized before its service method is called. 
 
When the client requests the servlet, the server passes that request to the container. Now the container processes that request and passes the response to the server. Here is a diagrammatic view of how the container handles the request: 
 
1. A client sends a request to the server and the server passes that request to the container.
JavaContainer1.jpg
2. The Container creates two objects, a request and a response 
JavaContainer2.jpg
3. The Container finds the servlet based on the URL and creates a thread of that servlet. It passes both the objects in the servlet thread. 
JavaContainer3.jpg
4. The Container calls the service() method of the servlet and depending upon the client request it calls the doGet() or doPost() method.  
JavaContainer4.jpg
5. The Servlet thread generates the output of the request and passes that result into the response object. Then it sends the object back to the container.  
JavaContainer5.jpg
6. After the service method completes, the thread dies and the container converts the response into a HttpResponse and passes it to the server, which then transfers it to the client.  
JavaContainer6.jpg


Similar Articles