2013-12-14 4 views
1

입력 텍스트가있어서 가격을 입력하는 데 사용됩니다. 사용자가 입력 가격 insertproducts.jsp입력 텍스트 유효성 검사 오류 - JSP

에 검증을 제출 버튼을 클릭 한 후 내가 doinsertproducts.jsp에 유효성 검사 코드가 있습니다 :

  1. 입력 텍스트를 작성해야합니다.
  2. 입력 텍스트는 숫자 여야합니다.
  3. 가격은 0보다 커야합니다.

여기에 첫 번째 코드입니다 :

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 가지 오류를 감지 할 수있는 솔루션이 있습니까?

+2

사이드 참고 :'경우 (price.equals ("") || 가격 == null이)를'-'가격 == null'에 연결할 수 있습니다. 'price == null' 때문에 NPE를 얻을 수 있습니다. 'if ("". equals (price))'를 수행하십시오. – Maroun

+0

@MarounMaroun은 여전히 ​​오류가 발생했습니다. 코드가 텍스트 입력란에서 문자 입력을 처리 할 수없는 것 같습니다. – noobprogrammer

+0

javascript/jquery (insertproducts.jsp)를 통해 처리 한 다음 doinsertproducts.jsp에서 null을 확인하고 가져와야합니다. 매개 변수. –

답변

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

@noobprogrammer 위 코드를 시도하고 어려움을 발견하면 저에게 질문하십시오. –