【问题标题】:java.lang.NumberFormatException: nulljava.lang.NumberFormatException:空
【发布时间】:2013-03-28 10:27:44
【问题描述】:

我创建了一个登录表单,其中包含每个字段的验证。当我单击提交按钮时,将调用函数 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>

【问题讨论】:

  • 当然,手机号码通常是 10+ 位数字 - 超过一个整数。如果是国际号码,情况会更糟!
  • 如果用户名是'); DELETE FROM stud; --会发生什么
  • 您不应将电话号码存储为数字类型。电话号码不像数学数字,例如,您想用它进行计算。如果电话号码以 0 开头怎么办? int 不会记住这一点。使用字符串类型来存储电话号码。

标签: java html jsp jdbc


【解决方案1】:

您正在重定向浏览器以在 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。我认为您可以通过将action="insert.jsp" 属性添加到form 标记,将onClick 属性更改为onSubmit 并删除

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

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

【讨论】:

  • 非常感谢您的帮助
  • 很高兴为您提供帮助,欢迎来到 Stack Overflow。如果此答案或任何其他答案解决了您的问题,请将该答案标记为已接受。
【解决方案2】:

如果你的手机号为空会怎样?

   int mobile=Integer.parseInt(request.getParameter("mobile"));

您要求Integer.parseInt() 解析一个空或空字符串,这导致了您的问题。

来自the doc

抛出: NumberFormatException - 如果字符串不包含可解析的整数。

您需要检查mobile 是否已填充,或者在未填充时处理该场景。

顺便说一句,我不会使用Integer 来存储手机号码。位数可能会导致溢出和/或您可能希望维护结构(例如国家代码和数字)等。

【讨论】:

  • 我不这么认为。这是空输入。因为参数不存在,所以他使用 window.location = "insert.jsp"。请参阅我的答案以了解更多详细信息。
  • 非常感谢您的帮助
【解决方案3】:

如果你使用parseInt(),你应该在某处捕获异常:

int mobile;
try {
  mobile = Integer.parseInt(request.getParameter("mobile"));
} catch (NumberFormatException e) {
  // do something
}

在您的情况下,request.getParameter("mobile") 可能返回 null。

编辑: 如前所述 - 将电话号码存储为整数可能不是一个好主意。请改用Long

【讨论】:

    【解决方案4】:

    首先,您的代码非常危险。您应该在服务器端检查 null 或空!使用 PreparedStatement 而不是 Statement。

    其次,代码window.location = "insert.jsp"; 不会像您期望的那样工作。 使用 action="insert.jsp" 将数据发送到该页面。然后,在你的js函数上,如果不满足条件则返回false,否则返回true;

    使用 onSubmit="return validate()" 代替 onClick 事件。

    【讨论】:

    • 非常感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多