I am a newbie in JSP's and servlets. I have to allow the user to key in some questions in a html table and pass the entered data to the servlet. The number of questions could vary. So, I would like to pass it as a list or array to the servlet. I would not prefer using hidden tag for every row as it would be tedious when the rows are dynamic.
Could someone please suggest an efficient way to pass all the questions entered in a table column? Here's the code snippet..
<body>
<form action="adminPageServlet" method="POST">
<table class="table editabletable table-bordered table-condensed table-responsive"
border="1">
<tr>
<td><div contenteditable>Question1</div></td>
</tr>
<tr>
<td><div contenteditable>Question2</div></td>
</tr>
</table>
<button class="btn btn-default" type="submit" id="loginBtn">Submit</button>
</form>
</body>
EDIT:
Thanks a lot . I am still unable to pass the data from the function to servlet. Could you please help? I am contantly getting an error alert message here - which textStatus:undefined and data:[Object object].
jQuery.ajax({
url:'adminPageServlet',
data:{"key1":"value1","key2":"value2"},
type:'POST',
contentType:"application/json; charset=utf-8",
dataType : 'json',
success:function(data, textStatus, jqXHR){
// access response data
alert("success");
},
error:function(data, textStatus, jqXHR){
console.log('Service call failed!');
alert("failed - data"+data+"textSTatus"+textStatus);
}
});
I tried converting the question to JSON string using
var jsonString = JSON.stringify(questions);
and tried passing this also. Not working. Could you please help?