I am sending a $.getJSON
(HTTP GET) request twice (with different data), one after another (lets say we have request1 and request2). I can see in the developer tool
it looks like the cookie JSESSIONID from the frist 2 requests (request1 and request2) come from the first time I visit the page (lets say there was a request0 sent to the server when it created this JSESSIONID).
This was not true. I have 2 applications deployed under the same domain on the same server. So when I was calling http://mydomain.com/app1/initpage the server created a session for app1 with id FD0D502635EEB67E3D36203E26CBB59A and sent this JSESSIONID in a cookie to the client. The client saved the cookie under the mydomain.com and the second time when I executed http://mydomain.com/app2/executeService, the client browser sent the JSESSIONID from the cookie in the request header. I received it on the server but this was not a session in the other app2.
This explains the fact that when I send the other two requests (request1' and request2') they have a sessionID created on the appropriate application.
Have a look more here:
Deploying multiple web apps in same server
Under what conditions is a JSESSIONID created?
As for the concrete answer to my question, it appears that you need to make the 1st request synchronized so you are always sure that you have the same session id in the following requests. The following requests after the 1st one, can be asynchronous though.