I'm working on a small Servlets & JSP application while learning web development.
I have a question regarding validations and passing values between jsp and servlets.
I have a "Beer" class, with a "rating" property (of type double).
The servlet that loads "edit.jsp" creates a Beer object and loads the current values from the DB.
BeerDAO beerdao = new BeerDAO();
Beer beer = beerdao.getBeer(id);
request.setAttribute("beer", beer);
In the JSP, the object is displayed in the following manner:
...
<td class="left">Beer Name:</td>
<td><input type="text" name="name" value="${beer.name}"/></td>
...
<td class="left">Rating:</td>
<td><input type="text" name="rating" value="${beer.rating}"/></td>
...
Now, when I submit the form to the "update" servlet, each property is validated. In the case of the "rating" property, I convert it to a double.
Should I find an error in the validation (ie: letters, instead of numbers for the rating value), I want to go back to the form with the values that the user typed and an error message. Thing is, I need a Beer object in the request to be displayed in the form, but I can't pass the "rating" value to it, because it's not of the correct type. So right now I'm seeding the user back to a form with an empty rating.
I'm guessing I'm going at it wrong. So, what would be the proper way to validate numbers and get back to the edit form?