My pressing question is how to best handle this exception in a JSP web form, however I believe the below might also be able to provide an answer.
I've been messing around with a java project which I have implemented as an applet/JFrame and also a web form via a JSP. In the last class I took (essentially Java 1), the method I learned for handling Exceptions such as NumberFormatException
when a user is entering data is via a try/catch block within an ActionListener
.
Example:
private ActionListener L_Weight() {
return new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
double weight = Person.DEFAULT_WEIGHT;
try {
weight = Double.parseDouble(ae.getActionCommand());
}
catch (NumberFormatException e) {
//e.printStackTrace();
}
person = new Person(
person.getGender(),
weight,
person.getAge(),
person.getNumberOfDrinks(),
person.getHoursSinceFirstDrink());
view.updatePerson(person);
}
};
}
So while this works for the applet/JFrame, it doesn't seem like the best usage of try/catch, I don't know what to do with the NumberFormatException other than ignore it, and it does nothing for me on the web form side.
So on the web side, I have found various ideas for handling exceptions/errors, but none seem like they are the best option. My current favorite is to add onsubmit="return checkForm(this)"
to my form submission, where checkForm()
is javascript code that will return true if valid data is submitted. This still seems like an extraneous step, but other than declaring the variables as Strings and parsing them as doubles so that I can do try/catch errorhandling on the java side, I'm not sure where else I can validate the submissions.
So I implemented the checkForm() function and it's working, just wondering if this is a good practice for form validation in a JSP:
<form name="Name Input Form" action="index.jsp" method="POST" onsubmit="return checkForm(this)">
<script type="text/javascript">
function checkForm(form) {
var result = true;
var numbers = /^[0-9|\.]+$/;
var checkFields = new Array(
form.age,
form.weight,
form.drinks,
form.hours);
for (i=0; i < checkFields.length; i++) {
if (!checkFields[i].value.match(numbers)) {
alert(checkFields[i].name + " field must be a number");
checkFields[i].focus();
result = false;
break;
}
}
return result;
}
function optionChange(form) {
if (checkForm(form)) {
form.submit();
}
}
I also made the gender selection a dropdown which calls the optionChange function:
<select name="gender" id="gender" onchange="optionChange(this.form)">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
You're welcome to check out the web app and the source
Edit:
After posting this questions I realize I also have another question related to this, would it be a poor choice to create a constructor in the Person class which takes the gender, weight, age, drinks, and hours as String parameters, then handles the conversion into their respective types? I could place the error handling into try/catch blocks here but again I would (as I see it) be exploiting the error handling to force validation. From here I wouldn't need to worry about the validation as the java code would expect Strings, but that would seem like an unnecessary amount of refractoring to get working.