title: Localstorage with java servlet
body: <p>I have a project that requires the user to log in.  This is done via a login page that uses an AJAX call to a servlet to validate the user.  The servlet responds with string that tells it where to go next (either the main page or an "unauthorized access" page.  If there's an error, it displays an alert with the stacktrace).  If the user tries to go to the main page without validating, I want it to redirect them back to the login page.</p>
  
  <p>I'm trying learn to use localStorage to set the validation so it carries from page to page within the system.  However, the localStorage doesn't seem to persist across pages.</p>
  
  <p>BTW, the company standard is IE9 (although most everyone is using IE11).</p>
  
  <p>On clicking the "Enter" button, the following JS code is fired:</p>
  
  <pre><code>    $.ajax({
      url: 'MyServlet',
      type: 'GET',
      data: {formType: 'validate',
          login: $('#txtLogin').val(),
          password: $('#txtPassword').val()
      },
      dataType: 'html',
      success: function (responseText) {
          if (responseText.indexOf(".html") &gt; -1) {
              localStorage.setItem("name","authorized");
              alert(localStorage.getItem("name"));
              location.href = "http://&lt;intraserver&gt;/newSite/" + responseText;
          } else {
              alert(responseText);
          }
      }
  });
  </code></pre>
  
  <p>When the main page is loaded, I have an onload call to:</p>
  
  <pre><code>        function checkValid() {
              try {
                  var name = localStorage.getItem("name");
                  alert(name);
                  if(name==null){
                      window.location.replace('http://MyServlet/MyProject');
                  }
              }
              catch (e) {
                  alert(e.message);
              }
          }
  </code></pre>
  
  <p>When the "Enter" button is clicked, I get the alert that says "authorized".  However, when the main page loads the "name" alert says null.</p>
  
  <p>What am I doing wrong?</p>