在JSP中将数据类型转换为INT时出错

时间:2012-08-20 10:17:06

标签: jsp type-conversion

我通过页面中的POST / GET方法从表单中收到数据。现在,默认情况下,JSP将其作为字符串。我想将它转换为int并尝试但它没有用。

我的JSP代码是:

String editstr=request.getParameter("some_thing");
int edit;
edit=Integer.parseInt(editstr)

我也试过像

这样的东西
int edit=Integer.parseInt(editstr)

无法正常工作

2 个答案:

答案 0 :(得分:1)

你得到NumberFormatException还是NullPointerException

如果没有,那么在设置和获取参数some_thing时会遇到麻烦。

答案 1 :(得分:1)

必须必须测试参数是否为空。

String editstr=request.getParameter("some_thing");
int edit=0;
if(editstr!=null){
   try{
     edit=Integer.parseInt(editstr);
   }catch(Exception ex) { 
     out.println(ex);
   }
}

PS:你应该学习/使用JSTL到avoid Java code in JSPs