输入文本验证错误-JSP



我有一个输入文本,假设它用于输入价格。用户点击insertproducts.jsp 上的提交按钮后,我在doinsertproducts.jsp上有验证码

输入价格的验证是:

  1. 必须填写输入文本
  2. 输入文本必须是数字
  3. 价格必须大于零

这是第一个代码:

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).");
    }

不管怎样,当我执行第一个代码并尝试将字符输入到输入文本中(不包括第二个代码)时,有一个根本原因错误:

Cannot call sendRedirect() after the response has been committed

代码似乎尚未处理该错误。有什么解决方案可以让代码检测到用户的3个错误吗?

试试这个:

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);
}

最新更新