title: Java Servlet & jQuery AJAX - unable to retrieve object from session
body: <p>Not sure how to solve this, need some help here.  </p>
  
  <p>Ajax call brings user information to servlet, I save user object in HttpSession and control goes back to Ajax from where i redirect control to next JSP page via controller servlet. However, if i try to retrieve object from HttpSession it is null .. not sure how to solve this issue. </p>
  
  <p>here is my code for firstservlet:</p>
  
  <pre><code>    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
      // get values from http request
      // persist "user" object to database 
  
      HttpSession session = request.getSession();    //
      session.setAttribute("user", user);            //setting session variable
  
          Gson gson = new Gson();
          JsonElement jsonElement = null;
          jsonElement = gson.toJsonTree("/nextservlet");
          response.setContentType("text/plain");
          PrintWriter out=response.getWriter();
  
  
  }
  </code></pre>
  
  <p>here is my Javascript / AJAX code to redirect request to nextservlet</p>
  
  <pre><code>    $.ajax({
                  type: 'POST',
                  url: ‘firstservlet’,       
                  dataType: 'json',
                  contentType: 'application/json',
                  data: JSON.stringify(quiz),
  
  
                  success: function(result) {
                    //result = /nextservlet 
                    window.location.href = result;
  
  
                  },
                  error:function(data,status,er) {
                      console.log("Error:",er);
                  }
                });
  </code></pre>
  
  <p>and finally control comes to nextservlet - where i would like to process data and  then show new JSP page.</p>
  
  <pre><code>  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
      response.setContentType("text/plain");
      response.setCharacterEncoding("UTF-8");
  
      HttpSession session = request.getSession();
      User user = session.getAttribute(“user”);        //&lt;--- this is NULL
  
      LOG.warning("User id is  : " + user.getId());    //&lt;--- hence error here
  
      RequestDispatcher dispatcher = request.getRequestDispatcher 
      ("/anotherpage.jsp");
      dispatcher.forward(request, response);
  </code></pre>
  
  <p>}</p>
  
  <p>is issue because i am using -> window.location.href = result  to send request to nextservlet .. and it goes to  doGet??  </p>