title: Validating numbers with Servlets and JSP
body: <p>I'm working on a small Servlets &amp; JSP application while learning web development.</p>
  
  <p>I have a question regarding validations and passing values between jsp and servlets.</p>
  
  <p>I have a "Beer" class, with a "rating" property (of type double).</p>
  
  <p>The servlet that loads "edit.jsp" creates a Beer object and loads the current values from the DB.</p>
  
  <pre><code>BeerDAO beerdao = new BeerDAO();
  Beer beer = beerdao.getBeer(id);            
  request.setAttribute("beer", beer);
  </code></pre>
  
  <p>In the JSP, the object is displayed in the following manner:</p>
  
  <pre><code>...
  &lt;td class="left"&gt;Beer Name:&lt;/td&gt;
  &lt;td&gt;&lt;input type="text" name="name" value="${beer.name}"/&gt;&lt;/td&gt;
  ...
  &lt;td class="left"&gt;Rating:&lt;/td&gt;
  &lt;td&gt;&lt;input type="text" name="rating" value="${beer.rating}"/&gt;&lt;/td&gt;
  ...
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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?</p>