我有一个输入文字让我们说它用于输入价格。
用户点击doinsertproducts.jsp
insertproducts.jsp
上有验证码
输入价格的验证是:
这是第一个代码:
if(price.equals("")||price==null){
response.sendRedirect("../insertproducts.jsp?insertproduct="+producttype+"&err=Price must be filled.");
return;
}
else{
try{
intprice=Integer.parseInt(price);
}
catch (Exception e) {
response.sendRedirect("../insertproducts.jsp?insertproduct="+producttype+"&err=Price must be numeric.");
}
}
我不知道在哪里放置第二个代码来检查输入是否小于1:
if(intprice<1){
response.sendRedirect("../insertproducts.jsp?insertproduct="+producttype+"&err=Price must be greater than 0 (zero).");
}
无论如何,当我执行第一个代码并尝试在inputtext中输入字符(不包括第二个代码)时,会出现根本原因错误:
Cannot call sendRedirect() after the response has been committed
看起来代码没有处理错误。 有没有解决方案,所以代码可以检测到用户的3个错误?
答案 0 :(得分:0)
试试这个:
String price = request.getParameter("price").toString();
String errorMessage = "";
if(price != null && price != ""){
try{
intPrice = Integer.parseInt(price);
if(intPrice > 0){
System.out.println("Price " + intPrice + " is greater to 0.");
}else{
System.out.println("Price " + intPrice + " is less then or equal to 0.");
}
}catch(Exception ex){
ex.getMessage();
errorMessage = "Price is not a Number.";
response.sendRedirect("../insertproducts.jsp?insertproduct="+producttype+"&err="+errorMessage);
}
}else{
errorMessage = "Price was not given.";
response.sendRedirect("../insertproducts.jsp?insertproduct="+producttype+"&err="+errorMessage);
}