title: angularjs 500 internal error
body: <p>I am writing a simple <strong>angularjs - J2EE</strong> application to fetch data from <strong>mysql server</strong> and display on the html page.
  I am calling the angular function on form submit as:</p>
  
  <pre><code>&lt;div id="register_form" ng-controller="MyDiaryLogin"&gt;
          &lt;form&gt;
              &lt;ul&gt;
                  &lt;li&gt;&lt;input type="text" name="firstname" ng-model="user.firstname" /&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;input type="text" name="lastname" ng-model="user.lastname" /&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;input type="text" name="email" ng-model="user.email" /&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;input type="password" name="password" ng-model="user.password" /&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;input type="password" name="confpass" ng-model="user.confpass" /&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;input type="text" name="age" ng-model="user.age" /&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;input type="text" name="occupation" ng-model="user.occupation" /&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;input type="button" name="register" ng-click="register()" value="REGISTER" /&gt;&lt;/li&gt;
              &lt;/ul&gt;
          &lt;/form&gt;
          &lt;p&gt;&lt;h2&gt;{{status}}&lt;/h2&gt;&lt;/p&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p><em>{{status}}</em> here is $scope property from my angular script.js:</p>
  
  <pre><code>mydiary.controller('MyDiaryLogin', function($scope, $http){
  $scope.user = {};
  $scope.register = function(){
      $http({
            method: 'POST',
            url: 'http://localhost:9091/Angular1/getData',
            header: {'Content-Type': 'application/json'},
            data: $scope.user
      }).success(function(data){
          $scope.status = data;
      });
  };
  </code></pre>
  
  <p>I have written a servlet to get data from database but the error comes before reaching to the servlet as I am printing some text on console in servlet.
  When I click on <em>REGISTER</em> button browser console shows an <strong>error</strong> as:</p>
  
  <blockquote>
    <p><strong>POST <a href="http://localhost:9091/Angular1/getData" rel="nofollow">http://localhost:9091/Angular1/getData</a> 500 (Internal Server Error)</strong></p>
  </blockquote>
  
  <p>and this error points to <code>angular.js</code> lib at some <code>xhr.send(...)</code> function:</p>
  
  <pre><code>if (responseType) {
      try {
        xhr.responseType = responseType;
      } catch (e) {
        // WebKit added support for the json responseType value on 09/03/2013
        // https://bugs.webkit.org/show_bug.cgi?id=73648. Versions of Safari prior to 7 are
        // known to throw when setting the value "json" as the response type. Other older
        // browsers implementing the responseType
        //
        // The json response type can be ignored if not supported, because JSON payloads are
        // parsed on the client-side regardless.
        if (responseType !== 'json') {
          throw e;
        }
      }
    }
  
    xhr.send(post || null);
  }
  </code></pre>
  
  <p>For your reference my <strong>servlet class's doPost method</strong>:</p>
  
  <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
      try{
          System.out.println("CALLING THE METHOD");
          Class.forName("com.mysql.jdbc.Driver");
          cn = DriverManager.getConnection("jdbc:mysql://localhost/mydiary","root","");
  
          String query = "SELECT * FROM users";
          pstmt = cn.prepareStatement(query);
          rs = pstmt.executeQuery();
  
          String json = new Gson().toJson(rs);
          response.setContentType("text/html");
          out = response.getWriter();
          out.write(json);
      }
      catch(Exception e){
          e.printStackTrace();
      }
      try{
          try{}
          finally{
              if(rs!=null)
                  rs.close();
              if(pstmt!=null)
                  pstmt.close();
              if(out!=null)
                  out.flush();
                  out.close();
              if(cn!=null)
                  cn.close();
          }
      }
      catch(Exception e1){
          e1.printStackTrace();
      }
  }
  </code></pre>
  
  <p>Please help me find the root of the problem and is there anything wrong with the complete application code I have written? I am not sure about <strong>how to use angular to display data/ return json format data from servlet?</strong></p>