8 Replies Latest reply: Feb 25, 2011 6:29 AM by tschodt RSS

    Session handling

    Prem
      Hi,

      I have some doubt about session handling in servlet

      1. Who creates session ?
      When first request come to the servlet , is it a browser which sends session id (jsesessionid) to the servlet or
      WebContainer/Server creates it ? If it is created by browser , plz provide me code for that.

      2. If i open up two firefox instance , I am getting same jsessionid at server side ? how it is possible ?


      Thanks
        • 1. Re: Session handling
          EJP
          1. Who creates session ?
          The container.
          When first request come to the servlet , is it a browser which sends session id (jsesessionid) to the servlet
          No
          or WebContainer/Server creates it?
          Yes.
          If it is created by browser, plz provide me code for that.
          That request doesn't make any sense. The code concerned would have been in the browser if it worked that way. But it doesn't.
          2. If i open up two firefox instance , I am getting same jsessionid at server side ? how it is possible ?
          Because the server is associating your IP address with a single session.
          • 2. Re: Session handling
            tschodt
            Prem wrote:
            2. If i open up two firefox instance , I am getting same jsessionid at server side ? how it is possible ?
            Firefox instances are really just windows of the same Firefox (if you kill one, they all die).
            Firefox includes any cookies it has for the context (host) when it sends the http request.
            • 3. Re: Session handling
              Prem
              Thanks for your feedback.

              But till i have some doubts that if I open up firfox as well as IE and will hit same URL then how two different session are created ?
              • 4. Re: Session handling
                EJP
                Because the server isn't associating your IP address with a single session ;-) tschodt's explanation is correct, not mine.
                • 5. Re: Session handling
                  tschodt
                  Prem wrote:
                  if I open up firefox as well as IE and will hit same URL then how two different session are created?
                  For Firefox and IE to share the same session they would have to share their cookies. They do not.
                  • 6. Re: Session handling
                    Prem
                    For Firefox and IE to share the same session they would have to share their cookies. They do not.
                    So How server come to know that request came from IE and FF. I mean to say that there must be something unique to be send by browser as an identity to server
                    • 7. Re: Session handling
                      EJP
                      On the first connection the browser doesn't have a cookie to send, so the server returns a cookie. The browser sends that cookie via all subsequent requests and connections to the same ip:port until it is told otherwise by the server via another cookie.

                      So when FF did that the first time it was returned a session cookie. When IE did that the first time it was returned another one. When another window of the same instance of FF did that for the first time it had the cookie so it sent it so it became part of the existing session.
                      • 8. Re: Session handling
                        tschodt
                        Prem wrote:
                        2. If i open up two firefox instance , I am getting same jsessionid at server side ? how it is possible ?
                        [url http://www.fusioncube.net/index.php/multiple-sessions-firefox]One way to stop that from happening.
                        That blog entry is over 4 years old, hopefully it will still work.