0

This is my servlet

public class TestServlet extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        LogFactory.getLog(getClass()).info(req.getParameter("number"));
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

Now I execute the following js in browser

console.clear();
for (var i = 0; i < 12; i++) {
  $.ajax('/test', {
    data: {
      number: i
    }
  });
}

and results are:

2016-09-15 19:35:26 INFO  TestServlet:17 - 0
2016-09-15 19:35:26 INFO  TestServlet:17 - 2
2016-09-15 19:35:26 INFO  TestServlet:17 - 1
2016-09-15 19:35:26 INFO  TestServlet:17 - 3
2016-09-15 19:35:26 INFO  TestServlet:17 - 4
2016-09-15 19:35:26 INFO  TestServlet:17 - 5
2016-09-15 19:35:28 INFO  TestServlet:17 - 8
2016-09-15 19:35:28 INFO  TestServlet:17 - 6
2016-09-15 19:35:28 INFO  TestServlet:17 - 7
2016-09-15 19:35:28 INFO  TestServlet:17 - 11
2016-09-15 19:35:28 INFO  TestServlet:17 - 10
2016-09-15 19:35:28 INFO  TestServlet:17 - 9

You can see that 6 requests came at 19:35:26 and other 6 at 19:35:28

I use default Tomcat and jquery settings. Why this can happen? Is there any way to configure this?

Boris
  • 1,054
  • 1
  • 13
  • 23

2 Answers2

1

Browser has a limitation on sending ajax requests and use a pool and a queue to send them.

So yes there is no rules. If it important to save the order use async:false but it has very bad impact on user interface.

MSS
  • 3,520
  • 24
  • 29
1

The browser limits it to 6 ajax requests at a time. You can trick it by separating the endpoints to different subdomains but that gets a little bit out of the question's scope. I don't think there's anything you can do in the browser to change that.

EDIT: See the comments, different ports should work as well

Jonas Grumann
  • 10,438
  • 2
  • 22
  • 40