age<input type="number" name="age">
Price<input type="number" name="price">
现在正在使用servlet
int age=Integer.parseInt(request.getParameter("age"));
double price=Double.parseDouble(request.getParameter("price");
现在,如果用户正在输入值,那么它可以正常工作,但是当用户不输入值时,它将给出异常。请帮我。
答案 0 :(得分:0)
您只需在名称下方添加逻辑,类似地,您也可以为其余字段添加
String ageVal = req.getParameter("age");
String priceVal = req.getParameter("price");
int age = 0;
double price = 0;
if ((ageVal == null) || (ageVal.equals(""))) {
System.out.println(("PROVIDE STUDENT NUMBER...");
} else {
try {
age = Integer.parseInt("ageVal");
} catch (NumberFormatException nfe) {
System.out.println("NumberFormatException="+nfe.getStackTrace());
}
}
if ((priceVal == null) || (priceVal.equals(""))) {
System.out.println(("PROVIDE STUDENT NUMBER...");
} else {
try {
price = Integer.parseInt("priceVal");
} catch (NumberFormatException nfe) {
System.out.println("NumberFormatException="+nfe.getStackTrace());
}
}