How to return JSON object to AngularJS using Java Servlet
10,608
Try using the javax.json.JsonObject
as follow:
JsonObject jo=Json.createObjectBuilder()
.add("id", testcaseId)
.add("title", testcase.getTestcaseName())
.add("testscenario", testcase.getTestcaseDescription()).build();
Then set the response content type to json and send your json object in the response:
response.setContentType("application/json");// set content to json
PrintWriter out = response.getWriter();
out.print(jo);
out.flush();
Related videos on Youtube
Comments
-
mityakoval over 1 year
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
request.setAttribute()
andrequest.getParameter()
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 ofabTestCtrl.js
: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'); }); }
and my
processRequest()
code from the servlet: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()); }
Can you please help me to process this request and finally return this poor JSON!
BTW, Servlet doesn't recognize
command
parameter. It getsnull
. But there is such parameter in AngularJS function.