我从angularjs控制器发送json对象并在HttpServlet中收集dopost()方法。成功发送请求但我无法解析该json对象。
Html code and angularjs code
<body ng-controller='myctrl'>
<input type="text" id="fname" name="fname" ng-model="fname"/>
<input type="button" value="Click" ng-click="chk()">
<script>
function myctrl($scope,$http)
{
$scope.chk=function(){
$http.post("http://localhost:7001/Angularjsprac/test",$scope.fname).success(function (data){
});
} }
Servlet code
@WebServlet("/test")
public class PracticeController extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
StringBuffer sb= new StringBuffer();
BufferedReader reader=req.getReader();
String line=null;
while( (line=reader.readLine())!=null)
{
sb.append(line);
}
JSONParser parser= new JSONParser();
JSONObject joUser = null;
try
{
joUser = (JSONObject) parser.parse(sb.toString());
}
catch(ParseException E)
{
System.out.println("parse exception");
E.printStackTrace();
}
String user = (String) joUser.get("fname");
System.out.println("user"+user);
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.write("A new user " + user + " has been created.");
out.flush();
out.close();
}
}
我在解析时遇到错误。
---------------------------更新-----------
Error
17:49:51,287 INFO [stdout] (http--0.0.0.0-7001-1) parse exception
17:49:51,288 ERROR [stderr] (http--0.0.0.0-7001-1) Unexpected character (s) at position 0.
17:49:51,289 ERROR [stderr] (http--0.0.0.0-7001-1) at org.json.simple.parser.Yylex.yylex(Unknown Source)
17:49:51,289 ERROR [stderr] (http--0.0.0.0-7001-1) at org.json.simple.parser.JSONParser.nextToken(Unknown Source)
17:49:51,290 ERROR [stderr] (http--0.0.0.0-7001-1) at org.json.simple.parser.JSONParser.parse(Unknown Source)
17:49:51,291 ERROR [stderr] (http--0.0.0.0-7001-1) at org.json.simple.parser.JSONParser.parse(Unknown Source)
17:49:51,292 ERROR [stderr] (http--0.0.0.0-7001-1) at org.json.simple.parser.JSONParser.parse(Unknown Source)
17:49:51,293 ERROR [stderr] (http--0.0.0.0-7001-1) at controllers.PracticeController.doPost(PracticeController.java:32)
17:49:51,294 ERROR [stderr] (http--0.0.0.0-7001-1) at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
17:49:51,294 ERROR [stderr] (http--0.0.0.0-7001-1) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
17:49:51,295 ERROR [stderr] (http--0.0.0.0-7001-1) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)
17:49:51,296 ERROR [stderr] (http--0.0.0.0-7001-1) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
17:49:51,297 ERROR [stderr] (http--0.0.0.0-7001-1) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
17:49:51,298 ERROR [stderr] (http--0.0.0.0-7001-1) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
17:49:51,299 ERROR [stderr] (http--0.0.0.0-7001-1) at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)
17:49:51,300 ERROR [stderr] (http--0.0.0.0-7001-1) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
17:49:51,301 ERROR [stderr] (http--0.0.0.0-7001-1) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
17:49:51,302 ERROR [stderr] (http--0.0.0.0-7001-1) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
17:49:51,303 ERROR [stderr] (http--0.0.0.0-7001-1) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)
17:49:51,304 ERROR [stderr] (http--0.0.0.0-7001-1) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
17:49:51,305 ERROR [stderr] (http--0.0.0.0-7001-1) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)
17:49:51,305 ERROR [stderr] (http--0.0.0.0-7001-1) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)
17:49:51,306 ERROR [stderr] (http--0.0.0.0-7001-1) at java.lang.Thread.run(Unknown Source)
17:49:51,306 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/Angularjsprac].[controllers.PracticeController]] (http--0.0.0.0-7001-1) Servlet.service() for servlet controllers.PracticeController threw exception: java.lang.NullPointerException
at controllers.PracticeController.doPost(PracticeController.java:39) [classes:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:754) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
at java.lang.Thread.run(Unknown Source) [rt.jar:1.7.0_25]
答案 0 :(得分:0)
您没有发送Json对象,请尝试在帖子请求中使用angular.toJson($scope.fname)