title: Java EE: Getting parameters from POST for a login form
body: <p>I am trying to implement a simple login servlet but it's not working properly.</p>
  
  <p><strong>What I wanted to know</strong> is how to pass the parameters using a HTTP POST. It already works with HTTP GET but the username and password are visible from the URL. It would be better to hide them in a POST.</p>
  
  <pre><code>&lt;form method="post" action="home" &gt;
    &lt;input name="username" class="form-login" title="Username" value="" size="30" maxlength="2048" /&gt;
    &lt;input name="password" type="password" class="form-login" title="Password" value="" size="30" maxlength="2048" /&gt;
    &lt;input type="submit" value="Connect"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>  &lt;servlet&gt;
      &lt;servlet-name&gt;home&lt;/servlet-name&gt;
      &lt;servlet-class&gt;controller.HomeController&lt;/servlet-class&gt;
    &lt;/servlet&gt;
  
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;home&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/home&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p><strong>Servlet:</strong></p>
  
  <pre><code>public class HomeController extends HttpServlet {
  
      private HttpSession session;
      private UserBean userBean;
  
      public void doPost(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
  
          UserBean user = new UserBean();
          String userName = request.getParameter("username");
          String password = request.getParameter("password");
  
          user.setUsername(userName);
          user.setPassword(password);
  
          user = UserDAO.login(user);
  
          dispatch(request, response, ApplicationRessource.getInstance().getHomePage());
      }
  
      protected void dispatch(HttpServletRequest request,
                  HttpServletResponse response, String page)
              throws javax.servlet.ServletException, java.io.IOException {
          RequestDispatcher dispatcher = getServletContext()
                  .getRequestDispatcher(page);
          dispatcher.forward(request, response);
      }
  }
  </code></pre>
  
  <p>The problem is that the <code>userName</code> and <code>password</code> strings are always empty, meaning that the parameters are never fetched from the POST. What am I doing wrong?</p>