26

As far as I know Java Servlets are handling multiple requests concurrently and I've searched through StackOverflow as well as Google, and confirmed what I thought. However I am quite confused right now, I wrote a simple servlets that seem to show blocking behaviour.

so I have a simple Servlet:

public class MyServlet extends HttpServlet 
{
    private static final long serialVersionUID = 2628320200587071622L;

    private static final Logger logger = Logger.getLogger(MyServlet.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
    {
        logger.info("[doGet] Test before");

        try {
            Thread.sleep(60000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        logger.info("[doGet] Test after");

        resp.setContentType("text/plain");
        resp.getWriter().write("OK");

    }
}

Then I have 2 browser windows, I opened at the same time that hit my Servlet. And the result is the first request blocking the 2nd one. The log also shows:

10:49:05,088 [http-8383-Processor14]  INFO MyServlet - [doGet] Test before
10:50:05,096 [http-8383-Processor14]  INFO MyServlet - [doGet] Test after
10:50:05,106 [http-8383-Processor22]  INFO MyServlet - [doGet] Test before
10:51:05,112 [http-8383-Processor22]  INFO MyServlet - [doGet] Test after

I feel like I am missing something ... Servlets supposed to be able to handle concurrent request, but it doesnt seem to be doing it. I also did the same as above on the service method instead of doGet and it does the same thing.

Any pointers?

Thanks

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
iamoverflown
  • 315
  • 3
  • 6
  • 1
    Your browser might serialise the requests, what happens if you try this from 2 different computers or with 2 different browsers? (i.e. iexploere and firefox) You can also check http access logs to see if requests arrive at your servlet container concurrently. – rsp Nov 04 '11 at 15:02
  • Can you show the `web.xml` and (assuming this is tomcat) the `server.xml`. – beny23 Nov 04 '11 at 15:10
  • @beny: I'm not sure how exactly that would be helpful in understanding the cause of the problem. Can you elaborate? – BalusC Nov 04 '11 at 15:15
  • @BalusC: Just for completeness really, maybe there would have been just the one worker thread (unlikely due to the log category http-8383-Processor14 and 22), or some esotheric filter in web.xml... – beny23 Nov 04 '11 at 15:37

1 Answers1

39

Your browser is apparently using the same HTTP connection in different windows. The servlet container uses a single thread per HTTP connection, not per HTTP request. You should run two physically different webbrowsers to test this properly. E.g. one Firefox and one Chrome.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • What makes you think that a single-thread-per-**connection** is being used here? The two requests are processed by different threads, couldn't it be that a single-thread-per-**request** policy is actually adopted by the server, whereas at client side the same TCP connection is being used without HTTP pipelining (so queued by the browser)? This would explain the sequential processing despite different threads involved – Luigi Cortese Mar 04 '16 at 15:39
  • @BalusC: Instead of polluting this thread, I considered raising a separate thread for this. Please see - http://stackoverflow.com/questions/37360731/how-does-a-servlet-container-synchronise-access-for-multiple-requests-to-a-parti – Farhan stands with Palestine May 21 '16 at 08:43