title: Losing Session Variable forwarding from jsp to servlet
body: <p>I'm struggling with a rather troublesome problem. I'm making a web application using the following jsp/servlets. </p>
  
  <p>GameServlet
  Scene.jsp
  PageServlet</p>
  
  <p>GameServlet creates a new HttpSession and then sets a number of attributes I expect to use later. </p>
  
  <p>GameServlet then forwards to Scene.jsp using the following code:</p>
  
  <pre><code>HttpSession session = request.getSession();
          session.setAttribute("cur_game", game);
          session.setAttribute("logic", logic);
          session.setAttribute("cur_scene", scene);
          session.setAttribute("session_id", session.getId());
          out.println("Loaded session attributes");
          if (debug == null) {
              String target = "/Scene.jsp";
              RequestDispatcher rd = request.getRequestDispatcher(target);
              rd.forward(request, response);
          }
  </code></pre>
  
  <p>In Scene.jsp I display the session id for later comparison. This works and also displays the other attributes in the session. </p>
  
  <p>Then a button calls a function using the following code:</p>
  
  <pre><code>&lt;script&gt;
  function gotoPage(target)
  {
      Str = "PageServlet?page=" + target;
      window.location = Str;
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>However PageServlet doesn't seem to recognise the previous session. I check with this with the following code:</p>
  
  <pre><code>HttpSession sesh = request.getSession(false);
  
          if(sesh==null)
              out.println("Session is Null!");
          else
              out.println("&lt;br/&gt;Session id: " + sesh.getId());
  </code></pre>
  
  <p>And currently it displays that "Session is Null"</p>
  
  <p>If anybody could provide some help it would greatly appreciated! </p>
  
  <p>Many Thanks</p>
  
  <p>Alex</p>