I am passing an object to the Java Servlet publishStory
using Angularjs $http
service. And however this same servlet code has worked fine with other requests but here I am getting the following error:
I have researched it on the internet but could not find solution to this particular case where I am using BufferedReader, as the same servlet works fine with other requests.
Here is my 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();
}
}
I am retrieving two request parameters welcomeStoryTitle
and welcomeStoryWords
in this servlet.
And below is my angularjs controller with a function implementing $http
service:
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(){});
};
}]);
I am getting this error where I am parsing the reader object, at line:
juser = (JSONObject)parser.parse(reader);
Could this be because of the size of a String being retrieved from request?
'At position 0' is a giveaway. You are trying to parse an empty response.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.