F5 and CTRL+F5 cause different behaviour in concur

2019-09-13 03:28发布

问题:

Premise 1: This question was marked as a duplicate of this one, where the accepted (only) answer is more or less that the browser is using the same HTTP connection for multiple requests, while the servlet container uses one thread per connection. This is half an answer here, I'm trying to understand why refresh and hard refresh have different behaviours. Clearly this is not a strictly Java question, still belongs to programming domain though: not a duplicate, not OT, imho. No need to close it, an interesting discussion could start here.

Premise 2: I understand the difference between refreshing your browser page with F5 and CTRL+F5, but that's not helping me anyway.

I have this servlet

public class MyServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    List<String> items = new ArrayList<>();

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        System.out.print(request.getSession().getId()+" accessing list");

        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        items.add(""+LocalTime.now().getSecond());

        System.out.println(items);

    }

    //irrelevant code
}

I invoke it from my browser one first time and wait (a bit more than) 4 seconds to get back a response and have my JSESSIONID associated to my browser:

18:39:59,823 INFO  [...] 5MYxcir_Pz5isCLmWtchZRCx accessing list
18:40:03,823 INFO  [...] [3]

Now, I open three tabs in the same browser, paste the servlet url in each of them and press enter in one after the other, in maybe two seconds. This is the output I get

18:41:33,534 INFO  [...] 5MYxcir_Pz5isCLmWtchZRCx accessing list
18:41:37,534 INFO  [...] [3, 37]
18:41:37,542 INFO  [...] 5MYxcir_Pz5isCLmWtchZRCx accessing list
18:41:41,543 INFO  [...] [3, 37, 41]
18:41:41,549 INFO  [...] 5MYxcir_Pz5isCLmWtchZRCx accessing list
18:41:45,549 INFO  [...] [3, 37, 41, 45]

It looks like every request is handled sequentially: for each request to be processed you must wait the previous one to be terminated.

But if I go back to the three tabs and quickly CTRL+F5 them, I get the behaviour I was expecting:

18:46:37,812 [...] 5MYxcir_Pz5isCLmWtchZRCx accessing list
18:46:38,838 [...] 5MYxcir_Pz5isCLmWtchZRCx accessing list
18:46:39,571 [...] 5MYxcir_Pz5isCLmWtchZRCx accessing list
18:46:41,815 [...] [3, 37, 41, 45, 41]
18:46:42,838 [...] [3, 37, 41, 45, 41, 42]
18:46:43,571 [...] [3, 37, 41, 45, 41, 42, 43]

each request is run in parallel. One last test, if I just F5 them, I go back to the unexpected sequential behaviour

18:50:10,581 [...] 5MYxcir_Pz5isCLmWtchZRCx accessing list
18:50:14,582 [...] [3, 37, 41, 45, 41, 42, 43, 14]
18:50:14,590 [...] 5MYxcir_Pz5isCLmWtchZRCx accessing list
18:50:18,591 [...] [3, 37, 41, 45, 41, 42, 43, 14, 18]
18:50:18,597 [...] 5MYxcir_Pz5isCLmWtchZRCx accessing list
18:50:22,597 [...] [3, 37, 41, 45, 41, 42, 43, 14, 18, 22]

Could anyone help me to understand what's happening behind the scenes? Why are my requests sometimes handled sequentially?

I'm under Windows, WildFly 8.x AS, Chrome or Firefox (same behaviour).

NOTE:

I used [...] to shorten the output, but it shows that every request is handled by a different thread (different Thread.currentThread().getName() values, to be precise). This means that it is not true that I'm having "one thread per connection", I'm getting even more puzzled...