title: AngularJS http POST to Servlet
body: <p>I went through a lot of StackOverflow answers and googled a lot but still could not find why my post request is not working. </p>
  
  <p>This is my jsp:</p>
  
  <pre><code>&lt;div class="container"&gt;
        &lt;form class="form-signin" ng-controller="MyController"&gt;
          &lt;h2 class="form-signin-heading"&gt;Please sign in&lt;/h2&gt;
          &lt;label for="username" class="sr-only"&gt;Username&lt;/label&gt;
          &lt;input type="text" id="username"  ng-model="user.name" class="form-control" placeholder="Username" required autofocus&gt;
          &lt;label for="password" class="sr-only"&gt;Password&lt;/label&gt;
          &lt;input type="password" ng-model="user.password" id="password" class="form-control" placeholder="Password" required&gt;
          &lt;button class="btn btn-lg btn-primary btn-block" ng-click="login()" type="submit"&gt;Sign in&lt;/button&gt;
        &lt;/form&gt;
      &lt;/div&gt; 
  </code></pre>
  
  <p>This is my controller:</p>
  
  <pre><code>app.controller('MyController', function($scope, $http) {
  
      $scope.login = function() {
          console.log($scope.user);
          $http({
              method : 'POST',
              url : 'login',
              data : $scope.user,
              headers: {
                  'Content-Type': 'application/json'
              }
          }).success(function(data) {
              console.log(data);
          }).error(function(data) {
              console.log(data);
          });
          console.log("POST done");
      };
  });
  </code></pre>
  
  <p>And my servlet:</p>
  
  <pre><code>protected void doPost(HttpServletRequest request,
              HttpServletResponse response) throws ServletException, IOException {
  
          System.out.println("inside do POST");
          Gson gson = new Gson();
          JsonParser parser = new JsonParser();
          JsonObject obj = (JsonObject) parser
                  .parse(request.getParameter("data"));
          Iterator it = (Iterator) obj.entrySet();
          while (it.hasNext()) {
              System.out.println(it.next());
          }
          System.out.println("over");
      }
  </code></pre>
  
  <p>I keep getting this Null pointer exception</p>
  
  <pre><code>java.lang.NullPointerException
      at java.io.StringReader.&lt;init&gt;(StringReader.java:50)
      at com.google.gson.JsonParser.parse(JsonParser.java:45)
      at com.zookeeperUI.controller.Login.doPost(Login.java:40)
  </code></pre>
  
  <p>Please tell me what am I doing wrong here .</p>