以下是我的剧本:
<head>
<script type="text/javascript">
function del_form()
{
var s_id=document.Delete.s_id;
if(s_id.value != "")
{
//window.alert("Please enter the ID!");
alert("Please enter the ID!");
return false;
}
return true;
}
</script>
形式如下:
<body>
<form name="Delete" method="post" onsubmit=" return del_form()" action="AdminDeleteShop2.jsp" >
Enter the ID of the Shop which is to be deleted : <input type="text" name ="s_id" id="s_id">
<input type="submit" value="Delete!"><br>
</form><br><br>
<form name="Back" method="post" action="MasterMenu.jsp">
<input type="submit" value="Back">
</form><br><br>
</body>
问题在于,如果不执行表单验证,它将进入下一页AdminDeleteShop2.jsp。 它给了我以下错误:
org.apache.jasper.JasperException: An exception occurred processing JSP page /AdminDeleteShop2.jsp at line 26 : int s_id = Integer.parseInt(s_id_string);
root cause : java.lang.NumberFormatException: For input string: ""
Connection con = DriverManager.getConnection( host, uName, uPass );
Scanner sc = new Scanner(System.in);
String s_id_string = (String)request.getParameter("s_id");
int s_id = Integer.parseInt(s_id_string);
Statement stmt2 = con.createStatement();
int check = stmt2.executeUpdate("delete from ShopSystem.Shop where s_id="+s_id+"");
if(check>0)
%> S_ID = <%= s_id %> has been deleted successfully.<br> <%
}
else
{ %> Sorry the action cannot be completed. Please check if valid S_ID was entered<br> <%
}
sc.close();
%>
<form name="Back" method="post" action="MasterMenu.jsp">
<input type="submit" value="Back">
</form><br><br>
答案 0 :(得分:1)
请在脚本中更改您的条件,如下所示
function del_form(){
var s_id=document.Delete.s_id;
if(s_id.value == "")
{
//window.alert("Please enter the ID!");
alert("Please enter the ID!");
return false;
}
return true;
}