gpt4 book ai didi

java.lang.NumberFormatException : null

转载 作者:行者123 更新时间:2023-11-30 04:26:48 25 4
gpt4 key购买 nike

我创建了一个登录表单,其中包含每个字段的验证。当我点击提交按钮时,将调用函数validation()来验证所有字段,验证成功后,它将重定向到另一个jsp页面,其中所有详细信息将被插入到Oracle数据库中。

但我收到“org.apache.jasper.JasperException:java.lang.NumberFormatException:null”异常。还有“服务器遇到内部错误(),导致其无法完成此请求”错误。我希望你能帮助我。

这是代码:

<html>
<head>
<script type="text/javascript">
function validate()
{

if(document.frm.username.value=="")
{
alert("Please enter Username");
document.frm.username.focus();
}

else if(document.frm.mobile.value=="")
{
alert("Please Enter your contact number");
document.frm.mobile.focus();
}

else
{
window.location = "insert.jsp";
}
}
</script>
</head>

<body>
<form name="frm">
<table>
<tr><td>User Name:</td><td><input type="text" name="username"></td></tr>
<tr><td>Contact Number:</td><td><input type="text" name="mobile"></td></tr>
<tr><td><input type="submit" value="Submit" onclick="validate()"></td><td></td></tr>
</table>
</form>
</body>

插入.jsp:

  <body>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%
Connection con=null;
int mobile=Integer.parseInt(request.getParameter("mobile"));
String username=request.getParameter("username");
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","manager");

Statement st=con.createStatement();
st.executeUpdate("insert into stud values("+mobile+",'"+username+"')");
out.println("Data is successfully inserted!");

}

catch(Exception e)
{
System.out.print(e);
}
%>
</body>

最佳答案

您正在重定向浏览器以在 insert.jsp 上执行 GET,但您没有向该新 URL 提供请求参数。因此,您的 JSP 获取 mobile 请求参数,即 null,然后尝试将其解析为整数,产生 NumberFormatException

您可以将请求参数附加到 URL,如下所示:

window.location = "insert.jsp?mobile=" + document.frm.mobile.value + "&username=" + document.frm.username.value;

但最好在 POST 请求中提交这些值,而不是在 GET 中。我认为您可以通过向 form 标记添加 action="insert.jsp" 属性,将 onClick 属性更改为 onSubmit 并删除

 else {
window.location = "insert.jsp";
}

因为这将允许浏览器恢复正常的表单提交。如果在关注空字段后将其与 return false; 语句结合使用,您将阻止浏览器提交表单。

关于java.lang.NumberFormatException : null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15679411/

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