1

By default, the servlet container loads only a single instance of a servlet. Requests serviced by the servlet are run in separate threads, but share the same instance. This enables applications to be responsive and scalable, since it requires fewer resources and uses them efficiently.

Almost everyone knows that this is the default threading model.

This question/discussion is a follow-up on this particular stackoverflow thread.

But the output happens to be completely different in my case,

May 21, 2016 1:57:59 PM com.stackoverflow.question8011138.MyServlet doGet
INFO: [doGet] Test before                        // 1st  thread

May 21, 2016 1:58:26 PM com.stackoverflow.question8011138.MyServlet doGet
INFO: [doGet] Test before                        // 2nd  thread

May 21, 2016 1:58:59 PM com.stackoverflow.question8011138.MyServlet doGet
INFO: [doGet] Test after                         // 1st  thread

May 21, 2016 1:59:26 PM com.stackoverflow.question8011138.MyServlet doGet
INFO: [doGet] Test after                         // 2nd  thread

Even though I hit the url mapped to this particlular servlet in 2 browser windows of the same browser, the difference b/w these 2 hitting ~1 second, but I see the below output

May 21, 2016 1:58:26 PM com.stackoverflow.question8011138.MyServlet doGet
INFO: [doGet] Test before                        // 2nd  thread

after seconds more than that previously mentioned ~1 second,

i.e to this,

May 21, 2016 1:57:59 PM com.stackoverflow.question8011138.MyServlet doGet
INFO: [doGet] Test before                        // 1st  thread

Why?

Clearly, from the output above, the second request is not served after the first has been served completely, but in b/w, contrary to what the OP to that question suggested.

What exactly is going on?

Why 2 different outputs to the same scenario?

The result/ output clearly shows that the server is not synchronizing the 2 requests.

Farhan stands with Palestine
  • 13,890
  • 13
  • 58
  • 105
  • Threads don't run synchronous. So there is no logical reason why you would expect request 1 to be handled before request 2. It's asynchronous, whichever finishes first is served first. Seemingly this was request 2 and not 1, but in another test it could well be request 1 that gets served first. This seems like normal behaviour. – selten98 May 21 '16 at 09:18
  • @selten98: You mean to say that the result posted by OP in this http://stackoverflow.com/questions/8011138/servlet-seems-to-handle-multiple-concurrent-browser-requests-synchronously is entirely wrong. – Farhan stands with Palestine May 21 '16 at 09:20
  • What I'm saying is that it doesn't sound logical to me that (s)he observed this behaviour. It could be that a specific Java application server would do this, however with threads it seems illogical that this would happen. It would seem that whatever is handled first would be sent back to the client first. That's the logic of threads. – selten98 May 21 '16 at 09:26
  • 1
    This all depends on the specific client and server used and their (default) configuration. It's quite possible that your browser and/or OS uses different HTTP connections in each window/tab. At least, the server side behavior indicates this. – BalusC May 21 '16 at 10:51
  • @BalusC: I am using Apache-TomEE-webprofile-1.7.4. Honestly speaking, I have never heard this terminology-HttpConnection. I have searched the web, but couldn't come with a concrete case regarding the same. Is there any setting so that my browser will use same HTTP Connection for that specific servlet in 2 different browser windows of the same browser. I am curious and at the same time quite anxious being unable to replicate the case posted by OP to the question in discussion. – Farhan stands with Palestine May 21 '16 at 10:55

0 Answers0