如何使用Javaservlet将JSON对象返回给AngularJS
我必须在我的项目中使用servlet编写一个控制器。我以前做过,但是我从来没有使用过AngularJS,所以我通过如何使用Javaservlet将JSON对象返回给AngularJS,java,javascript,json,angularjs,servlets,Java,Javascript,Json,Angularjs,Servlets,我必须在我的项目中使用servlet编写一个控制器。我以前做过,但是我从来没有使用过AngularJS,所以我通过request.setAttribute()和request.getParameter()将Java代码放在JSP页面中。但现在前端开发人员使用AngularJS,我必须给他返回一个JSON对象。我不知道怎么做。下面是abTestCtrl.js的代码: app.controller("abTestCtrl", function($scope, $location, $http) {
request.setAttribute()
和request.getParameter()
将Java代码放在JSP页面中。但现在前端开发人员使用AngularJS,我必须给他返回一个JSON对象。我不知道怎么做。下面是abTestCtrl.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');
});
}
以及我的processRequest()
来自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());
}
你能帮我处理这个请求并最终返回这个可怜的JSON吗
顺便说一句,Servlet无法识别
命令
参数。它得到null
。但是AngularJS函数中有这样的参数。尝试使用javax.json.JsonObject
如下:
JsonObject jo=Json.createObjectBuilder()
.add("id", testcaseId)
.add("title", testcase.getTestcaseName())
.add("testscenario", testcase.getTestcaseDescription()).build();
然后将响应内容类型设置为json,并在响应中发送json对象:
response.setContentType("application/json");// set content to json
PrintWriter out = response.getWriter();
out.print(jo);
out.flush();
帮什么忙?您只是在转储一些代码而没有说明错误。如果您遇到错误,请发布错误消息/堆栈跟踪,详细说明预期的内容和正在发生的情况。顺便说一句,如果我是你的话,我会使用JSON转换器。@Gimby引用:“但是现在前端开发人员使用AngularJS,我必须给他返回一个JSON对象。我不知道怎么做。”对我来说,很明显,我无法将JSON对象返回AngularJS。@Mitykova
request.getparameter()
对我来说也总是返回null。你是怎么解决的?@kittu问题出在AngularJS代码中。您应该清楚地发送参数。例如,而不是$http.get('http://localhost:8080/UIUM_IMT4003/ABTestController“,{command:'getTestCaseInfo',testcaseID:$scope.getParam})
您应该编写http://localhost:8080/UIUM_IMT4003/ABTestController?command=getTestCaseInfo&testcaseID=“+$scope.getParam+”
。我一做那件事就成功了。