title: Java servlet redirect has wrong URL and uses wrong HTTP method
body: <p>I have a form that is submitting a POST request to a servlet defined by <code>@WebServlet("/SignUp")</code>.</p>
  
  <p>I also have checks on the client side to make sure all of the form inputs are populated, but if they sneak by that and submit the form with an empty input I'd like the servlet to send them back <code>/Home</code> (i.e. index.jsp) as a fallback.</p>
  
  <p>I have the following for my SignUp servlet's doPost():</p>
  
  <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
      final String METHODNAME = "doGet";
      logger.entering(CLASSNAME, METHODNAME);
  
      String email = request.getParameter("email");
      String pass = request.getParameter("password");
      String cpass = request.getParameter("confirmPassword");
  
      String forwardJsp = "";
      if (StringUtils.isNotBlank(email) &amp;&amp; StringUtils.isNotBlank(pass)
              &amp;&amp; StringUtils.isNotBlank(cpass)) {
          forwardJsp = "/SignUpMoreInfo.jsp";
      } else {
          // one of the fields was empty
          request.setAttribute("signupFieldWasEmpty", true);
  
          forwardJsp = "/Home";
      }
  
      request.getRequestDispatcher(forwardJsp).forward(request, response);
      logger.exiting(CLASSNAME, METHODNAME);
  }
  </code></pre>
  
  <p>This all works on a basic level.  If they neglect to enter a value (i have yet to do any real checking, like .trim() or invalid characters), then they are send back to index.jsp (which backed by the /Home servlet).</p>
  
  <p>However, I have <strong>two problems</strong> with the way my code is working and I can't figure out how to get around them.</p>
  
  <ol>
  <li><p>My index.jsp is now being loaded as a POST request, rather than the usual GET.  If the user refreshes the page, they get a client warning about refreshing the page on a POST request which is really ugly and cumbersome on the homepage.</p></li>
  <li><p>The URL in the browser still has the address <code>http://localhost:8080/SignUp</code>, when I was expecting it to be <code>http://localhost:8080/Home</code>.  This is also ugly and kind of confusing to the user, as the url no longer properly represents the page they associate with the "Home" page of the site.</p></li>
  </ol>
  
  <p>How do I fix these issues I have with my Java backend code while maintaining as much MVC as possible?</p>