Java – How does a single servlet handle multiple requests from client side


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 ?

Best Solution

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.


I made this answer as community wiki.

Related Question