How does a single servlet handle multiple requests from client side

40,133

Solution 1

Struts/Spring frameworks are actually written on top of Servlet specification so doesn't matter what you use underneath it use Servlets.

You are right, Only single instance of Servlet is created, but that instance is shared across multiple threads. For this reason you should never have shared mutable states in your Servlets.

For example you have following servlet mapped to http://localhost/myservlet

class MySerlvet extends HttpServlet {

     public void doGet(HttpServletRequest req, HttpServletResponse res) {
          // Get Logic
     }    
}

The Web Server will have something similar (Not necessarily same) in its code.

MyServlet m = new MyServlet(); // This will be created once

// for each request for http://localhost/myservlet
executorService.submit(new RequestProcessingThread(m));

Solution 2

Each request is processed in a separated thread. This doesn't mean Tomcat creates a new thread per request. There is a pool of threads to process requests. Also there is a single instance for each servlet and this is the default case.(Some more information). Your servlet should be Thread Safe i.e. it should be stateless.

enter image description here

If your servlet implements SingleThreadModel interface, each thread uses separate instance of servlet. SingleThreadModel is deprecated, Don't use it.

SingleThreadModel

I made this answer as community wiki.

Solution 3

You don't create multiple instances of servlet. The servlet engine utilizes a separate thread from the thread pool for each request (up to some max number of Threads allocated).

The performance is relative to the number of threads, not the number of instances of the servlet.

For example, if there are 1000 requests, and the maximum threads that can be generated by servlet is 100, then there will be performance degradation.

In order to avoid this problem, we can use load balancer by putting multiple servers behind a load balancer. The load balancer should be configured to "route" the requests to any one of the servers based upon different parameters/settings (round robin distribution, load distribution etc.). The more load you need, the more servers you should add. However, this does send all traffic through the load balancer so it is important that this be redundant and failover safe.

Share:
40,133
shivam-shekhar
Author by

shivam-shekhar

Updated on January 17, 2020

Comments

  • shivam-shekhar
    shivam-shekhar over 4 years

    How does a single servlet handle multiple client requests coming in the form of user requests ? Based on the singleton design pattern I know we get a single instance of servlet created , but how does a single servlet handle millions of requests . Confused about the threading involved in it also.

    Also does any browser specifications or settings come handy out here for sending the requests across or generating the threads sent out for the requests.

    Is it the same for all frameworks or it differs say for example struts v/s springs ?