巧用try_catch实现验证是否是整型类型
String idstr = request.getParameter("id");if(null == idstr){request.setAttribute("error", "没有指定宠物编号");return "error.jsp";}int id = 0;try {id = Integer.valueOf(idstr);} catch(Exception e){request.setAttribute("error", "指定宠物编号必须是整数");return "error.jsp";} 这是在servlet接收的ID 先判断ID是否为空 为空跳转到错误页面然后再转换为 int 在转换时出现异常 同样跳转到错误页面 输出错误信息
页:
[1]