title: Session attribute is lost after invoking a client-side redirect
body: <p>Previously, the servlet uses <code>response.sendRedirect("pages/my_page.jsp?foo=bar");</code> without problem. Session attributes can be retrieved in the subsequent pages being redirected to.</p>
  
  <p>Currently, I am changing the way to send requests. Originally, the Javascript uses <code>myForm.submit();</code>, but I have now changed it to <code>jQuery.ajax("my_page.jsp?foo=bar", {...});</code>. Then, the servlet includes a URL in the JSON response instead of <code>response.sendRedirect()</code>, and in the <code>success</code> function, I use <code>window.location.replace(url);</code> to navigate to the new page. However, the saved session attributes cannot be fetched in subsequent pages.</p>
  
  <p>I have compared the session IDs by inserting <code>&lt;%= session.getId() %&gt;</code> in my JSP pages. They are the same. The issue here is <code>session.getAttribute("myAttribute_1")</code> returns <code>null</code> in the page redirected to.</p>
  
  <p>I am not sure if this matters, but I am in fact doing this task with more than 1 JSP pages:</p>
  
  <pre><code>A.jsp --- (redirect) ---&gt; B.jsp --- (redirect) ---&gt; C.jsp
  </code></pre>
  
  <p>In this case, how can I get the saved session attributes in <code>C.jsp</code>?</p>
  
  <hr>
  
  <h2>Edit</h2>
  
  <p>Below is the code snippet I use to save session attribute.</p>
  
  <pre><code>public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      HttpSession session = request.getSession(true);
  
      response.setContentType("application/json");
  
      CustomObject customObject = new CustomObject();
      // ...
      session.setAttribute("myAttribute_1", customObject);
  
      PrintWriter writer = response.getWriter();
      JsonObject json = new JsonObject();
      Gson gson = new GsonBuilder().setPrettyPrinting().create();
  
      json.addProperty("url", "next_page.jsp?foo=bar");
      writer.println(gson.toJson(json));
      writer.close();
  }
  </code></pre>
  
  <p>Below is the code snippet for redirecting.</p>
  
  <pre><code>function redirectHandler(data, currentUrl) {
      if (data &amp;&amp; data.hasOwnProperty('url')) {
          var redirectUrl = data.url;
          jQuery.get(redirectUrl).done(function(response) {
              redirectHandler(response, redirectUrl);
          });
      } else {
          window.location.replace(currentUrl);
      }
  }
  
  function sendFormData(method, url, form) {
      jQuery.ajax(url, {
          'method': method,
          'data': parseData(jQuery(form).serializeArray()),
          'success': function(data) {
              redirectHandler(data, window.location.href);
          }
      });
  }
  </code></pre>
  
  <hr>
  
  <h2>Result</h2>
  
  <p>I have reverted back to using <code>response.sendRedirect("pages/my_page.jsp?foo=bar")</code> in my servlet.</p>
  
  <p>On the client side, <code>jQuery.ajax()</code> is removed and the function <code>sendFormData()</code> is updated as follows.</p>
  
  <pre><code>function sendFormData(form) {
      var data = parseData(jQuery(form).serializeArray());
      var f = document.createElement('form');
  
      for (var key in data) {
          jQuery('&lt;input&gt;').attr({
              'type': 'hidden',
              'name': key,
              'value': data[key]
          }).appendTo(f);
      }
  
      f.setAttribute('method', form.getAttribute('method'));
      f.setAttribute('action', form.getAttribute('action'));
      f.submit();
  }
  </code></pre>
  
  <p>Whenever I want to submit a form, a <code>click</code> event handler is attached. It will call <code>sendFormData(myOriginalForm);</code> rather than <code>myOriginalForm.submit();</code> as I need to customize the data to be sent.</p>
  
  <p>Only by applying these simple changes, everything works again.</p>
  
  <p>Still, I am looking for an explanation on this weird behavior.</p>