Q:

Input text validation error - JSP

I have an input text let's say it's used to input price.
I have validation code on doinsertproducts.jsp after the user click submit button on insertproducts.jsp
The validation for input price is:

the input text must be filled.
the input text must be numeric.
the price must be greater than zero.

Here's the 1st code:
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.");
        }
    }

And I don't have an idea where did I have to put this second code to check if the input is less than 1:
if(intprice<1){
            response.sendRedirect("../insertproducts.jsp?insertproduct="+producttype+"&err=Price must be greater than 0 (zero).");
    }

Anyway, when I execute the 1st code and try to input character into the inputtext (without including the 2nd code), there's a root cause error:
Cannot call sendRedirect() after the response has been committed

Looks like the error hasn't been handled by the code.
Is there any solution so the code could detect 3 errors from the user?

A:

Try this:
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);
}

