TL DR The script is correct, and one session overrides the other, and both pages share the session; but it does not matter.
In the above example, you have two simultaneous anonymous anonymous requests.
In other words, the request is not unique; two shared pages will be returned. Both of these pages will have new JSESSIONIDs not because of the race, but because the requests themselves are anonymous and therefore, essentially, ask Tomcat to create new sessions.
Suppose page2 won the JSESSIONID speed contest and the browser now has page2 cookie. Then the user clicks on the action on page 1. I think you are correct that the request will be marked with cookie page2.
But so what?
Page1 cannot contain any information related to the session, and therefore does not contain information about the user. Therefore, actions from it cannot have any state associated with the session (the state has just been created). If there is no specific state associated with the session, then there is no problem with the "wrong" JSESSIONID.
Look at it differently: if the request for page2 was completely processed before the request on page 1, how will page1 be different? I do not see the differences. If there is no difference in the returned HTML between the two scripts, then it does not matter that its JSESSIONID has changed.
OTOH, if the user has already visited bar.com, requests for pages and page2 will be associated with the same JSESSIONID, the returned pages are correct, and all this is good in the world of foo.com.
One problem: If you have CSRF protection enabled. CSRF libraries modify all URLs on the returned page to include an additional parameter. The CSRF Security Library checks for all incoming requests that their security token matches JSESSIONID. If page1 uses the cookie for page 2, CSRF protection rejects the request as forged.
If you need to have one session in an iframe : use URL rewriting. It was originally intended to manage sessions when the browser does not accept cookies. It works well, but the urls look disgusting.