title: Sending a request and response from a jsp to a servlet
body: <p>I am writing a validation servlet for a registration form. The form is in a .jsp file and has the following layout:</p>
  
  <pre><code>        &lt;div class="content"&gt;
              &lt;h2 class="form"&gt;Register&lt;/h2&gt;
              &lt;form action="Register"&gt;
                  &lt;p&gt;
                      User name: &lt;br /&gt;
                      &lt;input type="text" name="username" /&gt;&lt;br /&gt; First Name: &lt;br /&gt;
                      &lt;input type="text" name="fist_name" /&gt;&lt;br /&gt; Last Name: &lt;br /&gt;
                      &lt;input type="text" name="last_name" /&gt;&lt;br /&gt; Email Address: &lt;br /&gt;
                      &lt;input type="text" name="email" /&gt;&lt;br /&gt; Password: &lt;br /&gt;
                      &lt;input type="password" name="password" /&gt;&lt;br /&gt; Retype password:
                      &lt;br /&gt;
                      &lt;input type="password" name="password2" /&gt;&lt;br /&gt; &lt;input
                          type="submit" value="Register" /&gt;
                  &lt;/p&gt;
              &lt;/form&gt;
  
              &lt;!-- end .content --&gt;
          &lt;/div&gt;
  </code></pre>
  
  <p>The validation code should check that each field meets a set of constraints and write back to the jsp error messages if it doesn't.</p>
  
  <p>ValidationServlet.java</p>
  
  <pre><code>protected void doPost(HttpServletRequest request,
              HttpServletResponse response) throws ServletException, IOException {
          // get request parameters for username and password
          String user = request.getParameter("username");
          String firstName = request.getParameter("first_name");
          String lastName = request.getParameter("last_name");
          String email = request.getParameter("email");   
          String pwd = request.getParameter("password");
          String pwd2 = request.getParameter("password2");
          // Validate parameters and forward them to the ejb
          Map&lt;String, String&gt; messages = new HashMap&lt;String, String&gt;();
  
          if(user != null) {
              // Validate user name
              if(user.trim().isEmpty()) {
                  messages.put("error", "Please enter username");
              } else if(!user.matches("^[a-zA-Z0-9]$")) {
                  messages.put("error", "Invalid username.");
              } else if(user.length() &gt; MAX_LEN){
                  messages.put("error", "Invalid username");
              } else {
                  // Username is valid
                  messages.put("success", "good");
              }
          }
          if(firstName != null) {
              // Validate first name
              if(firstName.trim().isEmpty()) {
                  messages.put("error", "Please enter your first name");
              } else if(!firstName.matches("^[a-zA-Z]$")) {
                  messages.put("error", "Invalid name. Use alpha values only.");
              } else if(firstName.length() &gt; MAX_LEN){
                  messages.put("error", "Invalid name. Name should be at most " + MAX_LEN + " characters long");
              } else {
                  // First name is valid
                  messages.put("success", "good");
              }
          }
          if(lastName != null) {
              // Validate last name
              if(lastName.trim().isEmpty()) {
                  messages.put("error", "Please enter your last name");
              } else if(!lastName.matches("^[a-zA-Z]$")) {
                  messages.put("error", "Invalid name. Use alpha values only.");
              } else if(lastName.length() &gt; MAX_LEN){
                  messages.put("error", "Invalid name. Name should be at most " + MAX_LEN + " characters long");
              } else {
                  // Last name is valid
                  messages.put("success", "good");
              }
          }
          if (email != null) {
              // Email is specified as request parameter, do the business logic here.
              if (email.trim().isEmpty()) {
                  messages.put("error", "Please enter email");
              } else if (!email.matches("([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)")) {
                  messages.put("error", "Invalid email, please try again.");
              } else {
                  // Email is valid
                  messages.put("success", "good");
              }
          }
          if(pwd != null) {
              // Validate password
              if(pwd.trim().isEmpty()) {
                  messages.put("error", "Please enter password");
              } else if(pwd.length() &gt; MAX_LEN){
                  messages.put("error", "Invalid password");
              } else {
                  // Password is valid
                  messages.put("success", "good");
              }
          }
          if(pwd2 != null) {
              // Validate password
              if(pwd2.trim().isEmpty()) {
                  messages.put("error", "Please enter password");
              } else if(pwd2.length() &gt; MAX_LEN){
                  messages.put("error", "Invalid password");
              } else if(!pwd2.equals(pwd)) {
                  messages.put("error", "Passwords do not match");
              } else {
                  // Password is valid
                  messages.put("success", "good");
              }
          }
          // Put messages in request scope so that it's accessible in EL by
          // ${messages}.
          request.setAttribute("messages", messages);
  
          // Forward request to JSP for display.
          request.getRequestDispatcher("login.jsp").forward(request,
                  response);
      }
  </code></pre>
  
  <p>My questions is how can I access each message from the hash map and display them accordingly before the respective input field in the jsp?</p>