当我尝试在Servlt中接受ID时,出现数字fornat异常
JSJP页面:
var associates = document.getElementsByName("resourceId");
//var associates=document.getElementById(elementId).value;
var id = "";
for ( var i = 0; i < associates.length; i++) {
if (associates[i].checked) {
//alert(associates[i]);
id = associates[i].value;
alert(id);
break;
}
}
我正在尝试从jsp文件接受id值并将其发送到我的dao类,但是当我尝试接受id的值时却遇到数字格式异常
Servlet:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("in dopost");
//number format exception in this line
int associateID = Integer.parseInt(request.getParameter("id"));
if (dao.deleteAssociate(associateID)) {
System.out.println("deleted");
}
// System.out.println(associateID);
}
最佳答案
您正在尝试获取HTML网页元素的“ id”,而不是JavaScript变量。
另一方面,我建议在执行instanceof
之前使用parseInt
运算符。
例如:
int associateID = -1;
Object obj = request.getParameter("id");
if(null != obj && obj instanceof Number) {
associateID = Integer.parseInt(request.getParameter("id"));
if (dao.deleteAssociate(associateID)) {
System.out.println("deleted");
}
}