以下是OneEmployee.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<form>
<td> <c:out value="${emp.getEmpid()}"/></td>
<td> <input type="text" id="fname" value="<c:out value="${emp.getFname()}"/>"></td>
<td> <input type="text" id="lname" value="<c:out value="${emp.getLname()}"/>"></td>
<td> <input type="text" id="dob" value="<c:out value="${emp.getDob()}"/>"></td>
<td> <input type="text" id="salary" value="<c:out value="${emp.getSalary()}"/>"></td>
<td><button type="button" class="btn btn-xs" onclick="update(<c:out value="${emp.getEmpid()}"/>)">Update</button></td>
</form>
以下是Welcome.jsp //主页JSP
<script>
function update(id) {
var empid = "?empid=" + id;
var formdata = "xyz";
var fname = document.getElementById("fname").value;
var lname = document.getElementById("lname").value;
var dob = document.getElementById("dob").value;
var salary = document.getElementById("salary").value;
var paramlist = "?empid=" + empid + "&fname=" + fname + "&lname=" + lname + "&dob=" + dob + "&salary=" + salary;
$.ajax({
type: "post",
data: formdata,
url: "UpdateEmployee" + paramlist,
success: function (data) {
$('#row' + id).html(data);
}
});
}
</script>
以下是java servlet UpdateEmployee.java
String id = request.getParameter("empid");
String fname = request.getParameter("fname");
String lname = request.getParameter("lname");
String dob = request.getParameter("dob");
String sal = request.getParameter("salary");
double salary = Double.parseDouble(sal);
int empid = Integer.parseInt(id);
我正在尝试从Welcome.jsp的update()中的OneEmployee.jsp获取输入数据,并将数据发送到servlet UpdateEmployee.java。 Glassfish服务器响应:
Warning: StandardWrapperValve[UpdateEmployee]: Servlet.service() for servlet UpdateEmployee threw exception
java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
有什么问题?这里出了什么问题?为什么OneEmployee.jsp将空字符串传递给Welcome.jsp,因此UpdateEmployee,jsp?
答案 0 :(得分:0)
您正在尝试解析空字符串,以便抛出异常:
问题是你从前端代码(ajax req)获得一个空值。 (在发送之前在浏览器控制台中重新检查)。
在你解析时,首先检查它是null
还是empty
返回0:
String id = request.getParameter("empid");
String fname = request.getParameter("fname");
String lname = request.getParameter("lname");
String dob = request.getParameter("dob");
String sal = request.getParameter("salary");
double salary = 0;
if (sal != null && sal.length() > 0) {
try{
salary = Double.parseDouble(sal);
}catch(Exception e) {
salary = 0;
}
}
int empid = 0;
if (id != null && id.length() > 0) {
try{
empid = Integer.parseInt(id);
}catch(Exception e) {
empid = 0;
}
}