I read that the web server instantiates servlet once and then calls its doGet()
and doPost()
methods for each requests to this servlet.Now if there are 100 simultaneous requests to this servlet and the web server has called doGet()
for one such request, will it block other 99 requests until the called doGet()
method returns?
-
1Related: http://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-session-variables-and-multithreading/3106909#3106909 – BalusC Oct 21 '12 at 12:01
6 Answers
No, it won't, it would just call doGet() on the same servlet instance in 100 different threads. The incoming requests would be blocked if there are no idle threads to process the requests.

- 23,934
- 10
- 61
- 84
No, in the normal case new Thread is created for each request.
You can set the number of threads for each in your servlet container.
You can also set the thread to be blocking.

- 5,212
- 1
- 18
- 10
All request will be handled in a separate thread of itself so it won't block.Whenever a new request comes the containder will spawn a new thread for handling it.
This what the Java Servlet Specification 3.0 says about Handling Request
The basic Servlet
interface defines a service method for handling client requests.
This method is called for each request that the servlet container routes to an instance
of a servlet.
The handling of concurrent requests to a Web application generally requires that the
Web Developer design servlets that can deal with multiple threads executing within
the service method at a particular time.
Generally the Web container handles concurrent requests to the same servlet
by
concurrent execution of the service method on different threads.

- 15,488
- 8
- 55
- 83
The web server spawns a thread to service each client. So even though you have one servlet each method will be called by a different thread for each client.
So as you can understand, no it will not block.
This is why you the developer are responsible to make sure your servlets are thread-safe

- 52,998
- 69
- 209
- 339
In general, it depends on the servlet container. Most, however, are multithreaded. They will call doGet()
100 times, in multiple threads, and multiple threads can thus be using the same servlet object at the same time. As such, you must make your servlets thread-safe, or your servlet will seem to work under low load, but then fall apart mysteriously when you start getting more requests.

- 224,562
- 31
- 268
- 324
No.
Because doGet and doPost are not synchronized.
the method signature is
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
It is therefore the onus is entirely upon you to take care of the multi-threaded scenarios others have mentioned here.
Yes
Say, down the way in code execution path, for example, let's say you invoke a method as follows in doGet() method
doGet(..){
ObjectApple.doRipe() --> ObjectWater.needMoreWater()
}
- --> indicates that first method calls/invokes the other
and in the implementation of ObjectWater you have a shared resource (which should be synchronized for thread safety sake) like a member variable, then of course all your other 99 threads will be blocked - iff it is a blocking resource/data structure.

- 181
- 1
- 3
- 12