table created using jquery is not accessible from servlet
Based on the help availed from stackoverflow I created the table with hiddenfields to pass the value to servlet, but in servlet I am unable to get the values of the input fields.
Here is my jQuery code to create table:
$("#linkInstr").click(function() {
var arr = new Array();
var cdid = $("#cboinstr option:selected");
var code = $("#cbocode option:selected");
$.get("trnDC?caseNo=21&insid="+cdid.text(), function(data) {
arr = data.split(",");
var contents = '<tr><td><input type="checkbox" id="chk_select'+counter+'" /></td><td><input type="hidden" id="txtCodeid'+counter+'" value="'+code.text()+'"/> ' + code.text()+ '</td><td><input type="hidden" id="txtInstrid'+counter+'" value="'+cdid.text()+'"/>' + cdid.text() + '</td><td>' + arr[0] + '</td><td>' + arr[1] + '</td><td>' + arr[2] + '</td>开发者_Python百科</tr>';
alert(contents);
$("#tblDetails").append(contents);
counter++;
})
})
And here is my Servlet code:
int noOfRows = Integer.parseInt(request.getParameter("noOfRows"));
for (int i = 0; i < noOfRows; i++) {
int j = i + 1;
String codeid = request.getParameter("txtCodeid" + i);
throw new Exception(request.getParameter("txtCodeid" + i));
String instrId = request.getParameter("txtInstrid"+i);
st.executeUpdate("insert into trndcdtls values(" +ccode +"," +fyear_code +"," +Dcno +"," + j +",'" + codeid +"','" + instrId + "','"+status+"')");
}
The exception is throwed purposefully by myself to check any value is availble on request, but it shows nothing.
You need to give the input fields a name. This becomes the request parameter name.
Thus, instead of
<input id="foo">
<input id="bar">
you need
<input name="foo">
<input name="bar">
The ID is purely to identify the elements at the client side, not at the server side. You may add them, but they won't be sent to the server side. Only the name
-value
pairs of an input element will be sent as request parameters to the server side.
That said, please use PreparedStatement
in your Java code to avoid SQL injection risks. Further on, it surprises me that you got the Java code compiled with the Exception in the middle of the flow. It would have given "unreachable code" error.
精彩评论