title: JSP Validating and Redirecting: how to validate form input and forward the errors back to the original page?
body: <p>I'm taking a class on JSP and I have an assignment... we have to write a JSP page that takes user input, validate the input and then forward it to a different web site.  To be more precise, we were asked to implement a rudimentary version of the FareFinder functionality of Amtrak's web site.</p>
  
  <p>There are 2 main purposes to this assignment:<br>
  (a) to write JSP which performs as middleware;<br>
  and (b) to write JSP which validates form data.</p>
  
  <p>I have a general question about the principles of doing the validation.  Currently I have a JSP that has a form and a submit button.  When the user clicks on the submit button I forward them to Validate.jsp.  The Validate.jsp will then validate the data and if the input is OK it will automatically redirect the request to the Amtrak web site with all the parameters filled out.</p>
  
  <p><a href="http://pastie.org/816784" rel="noreferrer">FareFinder.jsp</a> -> <a href="http://pastie.org/816792" rel="noreferrer">Validate.jsp</a> -> Amtrak<br>
  (click on the file name to see all my code in a pastie)</p>
  
  <p>Briefly, the main thing that I'm doing FareFinder.jsp:</p>
  
  <pre><code>&lt;FORM METHOD=POST ACTION="Validate.jsp"&gt;
      &lt;!-- all the input fields are up here --&gt;
      &lt;P&gt;&lt;INPUT TYPE=SUBMIT&gt;&lt;/P&gt;
  &lt;/FORM&gt;
  </code></pre>
  
  <p>The main thing I'm doing in Validate.jsp:</p>
  
  <pre><code>&lt;%@ page import="java.util.*" import="java.io.*"%&gt;
  &lt;%
      // retreive all the parameters
      String origin = request.getParameter("_origin");
      String depmonthyear = request.getParameter("_depmonthyear");
      String depday = request.getParameter("_depday");
      String dephourmin = request.getParameter("_dephourmin");
      String destination = request.getParameter("_destination");
      String retmonthyear = request.getParameter("_retmonthyear");
      String retday = request.getParameter("_retday");
      String rethourmin = request.getParameter("_rethourmin");
      String adults = request.getParameter("_adults");
      String children = request.getParameter("_children");
      String infants = request.getParameter("_infants");
      String searchBy = request.getParameter("_searchBy");
  
      // validate the data
  
      // redirect to Amtrak or back to FareFinder.jsp
  %&gt;
  </code></pre>
  
  <p>I have several questions:<br>
  How do I return to FareFinder.jsp from Validate.jsp and reflect the errors found in the validation page?<br>
  Once I have found errors- do I redirect the response back to FareFinder.jsp?<br>
  How could I transmit the error(s) back to FareFinder.jsp?  </p>
  
  <p>A generic answer would be fine too, but I'm giving my code as an example.  </p>
  
  <p><b>Note: the validation must be performed on the server side and I can't use javascript.</b></p>