gpt4 book ai didi

java - 我遇到数字格式异常

转载 作者:行者123 更新时间:2023-12-03 12:11:50 25 4
gpt4 key购买 nike

当我尝试在servelt中接受id时,我收到数字格式异常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 变量。

另一方面,我建议在执行 parseInt 之前使用 instanceof 运算符。

例如:

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");
}
}

关于java - 我遇到数字格式异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24933975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com