title: How the creation of HTTPSession works when request is coming from webserver instead of web browser?
body: <p>I have a very basic question how the creation of HTTPSession works.I know you folks will fire me on looking at this question as similar kind
  of questions exist.But there is reasoning why i am asking this question Here it is :-</p>
  
  <p>I know httpsession is unique to web browser and server creates it when we do HttpServletRequest.getSession first time.It will maintaintain the same session till we
  close the browser. But i have little bit different scenario.I Have a web application on one tomcat instance say T1.On welcome page of this web application
  i have provided two links on click of which takes me to same java servlet(S1)  of different web application hosted on another tomcat instance T2 (these two links
  opens two seperate pop up windows). Now first i click the link1 and inspect the sessionId in S1 and find its value as 1678.  Now first i click the link2 and 
  inspect the sessionId in S1 and find its value  again as 1678. <strong>My question here is why i am getting the same session id for both the requests origintaing
  from link1 and link2? what can i do to to get the different session for both of these requests?</strong></p>
  
  <p><strong>What i tried after looking for possible solutions on net :-</strong> On click of link1, in Servlet S1 , i copied session attributes, invalidate it and create  new one.
  Say new session id is 8765 . Now i click the link2 and found the same session  in this request too. So  i further invalidate it and creates new one(say 
  new session id is 4897). Ideally it should expire the first browser session (generated on click of link1). To verify it,i click anywhere on pop up 1 it does not get
  expired but i see again last generated session id i.e 4897. I am not getting why it attaching the same session id with both pop up windows?</p>
  
  <p><strong>Folks Thanks for your patience for taking your time out and read this long scenario?</strong></p>
  
  <p><strong>Edit :-</strong> </p>
  
  <pre><code>      Cookie[] cookies = req.getCookies(); 
          if(cookies!=null) 
          for (int i = 0; i &lt; cookies.length; i++) { 
           cookies[i].setMaxAge(0); 
           context.getResponse().getHttpServletResponse().addCookie(cookies[i]);
          } 
  
      HttpSession myAppSession = req.getSession();//line 1
  </code></pre>
  
  <p>Assume on click of link1 i get session id as 1234,then after click of link 2 also i get the same session id. As per my understanding, after executing the code above line 1 , i should get the different session id as i am setting the MaxAge as0 before getting the session. But its not happening?</p>