title: How to return JSON object to AngularJS using Java Servlet
body: <p>I have to write a controller in my project using servlets. I've done it before but I've never worked with AngularJS, so I did it via <code>request.setAttribute()</code> and <code>request.getParameter()</code> and put Java code inside of a JSP page. But now frontend developer used AngularJS and I have to return him a JSON object. And I have no idea how to do it. Here's the code of <code>abTestCtrl.js</code>:</p>
  
  <pre><code>app.controller("abTestCtrl", function($scope, $location, $http) {
          $scope.title = "no title";
          $scope.description = "no description";
      $scope.getParam = $location.search()['id'];
      if($scope.getParam === undefined)$scope.getParam = 0; 
  
      //$scope.getParam=2;
      //path: localhost8080/UIUM.../servlet-name.java
          //with two ids
          //web.xml: serverlet mapping for the path
          if($scope.getParam==='0'||$scope.getParam === 0){
              var saveButton = document.getElementById("saveButton");
              saveButton.classList.remove("hidden");
          }
          else{
              $http.get('http://localhost:8080/UIUM_IMT4003/ABTestController', {command:'getTestCaseInfo', testcaseID:$scope.getParam}).
              success(function(data, status, headers, config) {
                // this callback will be called asynchronously
                // when the response is available
                console.log('request succesful');
                console.log(data);
                console.log(status);
                console.log(headers);
                console.log(config);
              }).
              error(function(data, status, headers, config) {
                // called asynchronously if an error occurs
                // or server returns response with an error status.
                console.log('request not succesful');
              });
          }
  </code></pre>
  
  <p>and my <code>processRequest()</code> code from the servlet:</p>
  
  <pre><code>protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, ClassNotFoundException {
          response.setStatus(HttpServletResponse.SC_OK);
          response.setContentType("application/json; charset=UTF-8");
          //PrintWriter printout = response.getWriter();
  
          JSONObject jObject = null;
          RequestDispatcher view = null;
          TestcaseRepository testcaseRepo = new TestcaseRepository();
  
          String command = request.getParameter("command");
  
          if(command == null)
          {
              view = request.getRequestDispatcher("/testcases.jsp");
              view.forward(request, response);
          }
  
          if(command.equals("getTestCaseInfo")){
              String testcaseId = request.getParameter("testcaseID");
              Testcase testcase = testcaseRepo.getTestcaseById(testcaseId);
              jObject = new JSONObject();
              jObject.put("id", testcaseId);
              jObject.put("title", testcase.getTestcaseName());
              jObject.put("testscenario", testcase.getTestcaseDescription());
  //            printout.print(jObject);
  //            printout.flush();
              jObject.write(response.getWriter());
          }       
  </code></pre>
  
  <p>Can you please help me to process this request and finally return this poor JSON! </p>
  
  <p>BTW, Servlet doesn't recognize <code>command</code> parameter. It gets <code>null</code>. But there is such parameter in AngularJS function.</p>