我得到数字格式异常

时间:2014-07-24 12:41:26

标签: java javascript

当我试图接受servelt中的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文件中加入值并将其发送到我的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);
}

1 个答案:

答案 0 :(得分:1)

您正在尝试获取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");
    }
}