Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 968
shivam-shekhar Avatar asked Jan 04 '15 12:01

shivam-shekhar


People also ask

Can servlet handle multiple requests?

A Java servlet container / web server is typically multithreaded. That means, that multiple requests to the same servlet may be executed at the same time.

How do servlets handle multiple simultaneous requests?

Servlets handle multiple simultaneous requests by using threads.

How do you handle multiple requests at the same time in Java?

Your best option is to use ExecuterService which is kind of managed thread pool where you can specify thread pool size and submit multiple Runnable or Callable objects for processing.

How an HTTP servlet handles its client requests?

An HTTP Servlet handles client requests through its service method. The service method supports standard HTTP client requests by dispatching each request to a method designed to handle that request. For example, the service method calls the doGet method shown earlier in the simple example servlet.


2 Answers

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.

like image 64
5 revs, 3 users 87% Avatar answered Oct 05 '22 18:10

5 revs, 3 users 87%


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)); 
like image 43
shazin Avatar answered Oct 05 '22 18:10

shazin