【发布时间】:2016-06-11 08:52:42
【问题描述】:
我正在使用 Angularjs $http 服务将一个对象传递给 Java Servlet publishStory。然而,同样的 servlet 代码在其他请求中运行良好,但在这里我收到以下错误:
我在互联网上对其进行了研究,但在我使用 BufferedReader 的这个特殊情况下找不到解决方案,因为同一个 servlet 可以很好地处理其他请求。
这是我的publishStory servlet:
public class publishStory extends HttpServlet{
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
PreparedStatement pstmt = null;
Connection cn = null;
ResultSet rs = null;
boolean flag = false;
PrintWriter out = null;
try{
System.out.println("CALLING THE METHOD");
Class.forName("com.mysql.jdbc.Driver");
cn = DriverManager.getConnection("jdbc:mysql://localhost/storyboard","root","");
BufferedReader reader = request.getReader();
JSONParser parser = new JSONParser();
JSONObject juser = null;
juser = (JSONObject)parser.parse(reader);
String welcomeStoryTitle = (String)juser.get("welcomeStoryTitle");
String welcomeStoryWords = (String)juser.get("welcomeStoryWords");
String query = "INSERT INTO stories (storytitle, storytext) VALUES (?,?)";
pstmt = cn.prepareStatement(query);
pstmt.setString(1, welcomeStoryTitle);
pstmt.setString(2, welcomeStoryWords);
int i = pstmt.executeUpdate();
if(i>0)
System.out.println(welcomeStoryTitle=" : "+welcomeStoryWords);
else
System.out.println("No data inserted");
}
catch(Exception e){
e.printStackTrace();
}
}
我在这个 servlet 中检索两个请求参数 welcomeStoryTitle 和 welcomeStoryWords。
下面是我的 angularjs 控制器,它有一个实现$http 服务的函数:
app.controller('welcomeStoryPublishCtrl', ['$log','$http',function($log,$http){
this.publishGroup = function(wsPublish){
$http({
method:'POST',
url: 'http://localhost:9090/Writer/publishStory',
header: {'Content-Type':'application/json'},
data: wsPublish
}).success(function(){});
};
}]);
我在解析阅读器对象时遇到此错误:
juser = (JSONObject)parser.parse(reader);
这可能是因为从请求中检索到的字符串的大小?
【问题讨论】:
标签: java angularjs json servlets