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");
现在,如果用户输入了值,那么它工作正常,但是当用户没有输入时,它会给出异常。请帮助我。
您可以简单地为名称添加以下逻辑,同样您也可以添加其余字段
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());
}
}