【问题标题】:HTTP Status 500 - Exception occuring at string to int conversionHTTP 状态 500 - 字符串到 int 转换时出现异常
【发布时间】:2014-08-04 08:57:43
【问题描述】:

环境:我在 eclipse kepler 中工作,我的 MySQL 数据库在 AWS RDS 上。我的目标是创建一个基于 Web 的项目,因此我的项目是“AWS Java Web 项目”。同样对于数据库连接,我已经将 mysql-connector-*-bin.jar 文件添加到我的构建路径中。

下面的代码当我作为一个简单的 java 应用程序运行时,它运行良好,但是当我在 Tomcat 服务器上运行它时,它显示以下错误:

org.apache.jasper.JasperException: 在第 114 行处理 JSP 页面 /WelcomeCustomer.jsp 时发生异常

第 114 行:

     String s_id_string=(String)request.getParameter("s_id");       
    int s_id = Integer.parseInt(s_id_string);

根本原因

java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
org.apache.jsp.WelcomeCustomer_jsp._jspService(WelcomeCustomer_jsp.java:180)

第 180 行:

o_insert.setString(4, Cust_contact_string);

注意:Cust_contact_string 是字符串类型。

请帮助我,我似乎无法解决这个错误!

提前致谢

更新:

<form name="s_id_form" method="post" onsubmit=" return s_id_form()"> 
Choose a Shop from above by entering the respective s_id : 
<input type="text" name="s_id" id="s_id"> <input type="submit" value="submit"> 
</form>     
<%  
String s_id_string=(String)request.getParameter("s_id");    
int s_id = Integer.parseInt(s_id_string);   
%> 

【问题讨论】:

  • 更多参考我粘贴整个代码:
  • 您不能将 null 解析为数字。所以在转换(解析)之前确定是否为null。
  • 是的,我正在通过如下的 javascript 代码检查它是否为空:function s_id_form() { var s_id = document.s_id_form.s_id; if(s_id.value == "") { window.alert("请输入店铺ID!");返回假; } 返回真; }
  • 不要将代码作为评论发布,而是用它们更新您的帖子。

标签: java mysql eclipse jsp amazon-rds


【解决方案1】:

发生这种情况是因为s_id_stringnull。将您的代码修改为,

String s_id_string=(String)request.getParameter("s_id");       
int s_id=0;
if(s_id_string !=null || s_id_string !=""){
  s_id = Integer.parseInt(s_id_string);
}
else{
  s_id=0;//assign any value
}

【讨论】:

  • 谢谢它的工作..,对不起,我还没有足够的声誉来支持你..,一定会及时这样做..:)
【解决方案2】:

不要相信客户端验证。始终同时进行服务器端验证。

对于所提供的输入 (null),异常是显而易见的。

请在继续之前检查null。以及检查""

在提供的检查之后,将您的代码包装在 try catch block 中,以便在奇怪的输入传递到服务器端时执行某些操作。

【讨论】:

    【解决方案3】:

    处理JSP页面/WelcomeCustomer.jsp发生异常

    我建议你避免使用 Scriplet 并使用 JSP Standard Tag LibraryExpression language 来避免此类错误。

    ${param.s_id}
    

    阅读更多关于JSP - Implicit Objects

    param: 将请求参数名称映射到单个值


    您可以使用&lt;c:if&gt;&lt;c:choose&gt; 来检查nullempty 值。

    <c:if test="${empty param.s_id}">
        s_id is empty or null.
    </c:if>
    <c:if test="${not empty var1}">
        s_id is NOT empty or null.
    </c:if>
    

    <c:choose>
        <c:when test="${empty param.s_id}">
            s_id is empty or null.
        </c:when>
        <c:otherwise>
            s_id is NOT empty or null.
        </c:otherwise>
    </c:choose>
    

    ${not empty param.s_id}也可以由${!empty param.s_id}完成。

    Learn more...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-21
      • 2013-06-29
      • 2017-06-25
      • 1970-01-01
      • 2013-02-04
      • 2015-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多