title: Password validation with JSP and Servlet
body: <p>I have a user Sign in Html form where I get the user's email and password and check them against a database. So far I have the following code but when I submit the form it does not go to the specified JSP page. What can I do to improve my code and how can I just generate an error message when the user presses submit but still stay on the same page?</p>
  
  <p>Thank you in advance.</p>
  
  <p>//SERVLET doPost Method</p>
  
  <pre><code>    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  String userinp = request.getParameter("userinp"); //hidden type in html sign up form
          HttpSession s = request.getSession();
          User u = new User();
          if(userinp.equals("signup")) {
              u.setName(request.getParameter("name"));
              u.setLname(request.getParameter("lname"));
              u.setEmail(request.getParameter("email"));
              u.setPassword(request.getParameter("password"));
              s.setAttribute("User", u);
              //Save to DB
              u = (User)s.getAttribute("User");
              s.invalidate();
              UserM ud = new UserM(); //class which contains CRUD methods
              ud.createTable();
              ud.insert(u);
              ServletContext ctx = request.getSession().getServletContext();
              forwardTo(ctx, request, response, "/Somepage.jsp");
  
          } else if(userinp.equals("login")) {
              String pass1 = request.getParameter("pass");
              String email = request.getParameter("useremail");
              Connection conn = null;
              PreparedStatement stm = null;
              try {
                  conn = ConnectionConfiguration.getConnection();
                  stm = conn.prepareStatement("SELECT password FROM users WHERE email = ?");
                  stm.setString(4, email);
                  ResultSet resultSet = stm.executeQuery();
                  while(resultSet.next()) {
                      String pass2 = resultSet.getString("password");
                      if(pass1.equals(pass2)) {
                          ServletContext ctx = request.getSession().getServletContext();
                          forwardTo(ctx, request, response, "/Somepage.jsp");
                          } else {
                              //code to generate "Wrong Password" message
                          }
                      }
                  } catch(Exception e) {
                      e.printStackTrace();
                  } finally {
                      if(stm != null) {
                          try {
                              stm.close();
                          } catch (SQLException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
                      }
                      if(conn != null) {
                          try {
                              conn.close();
                          } catch (SQLException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
                      }
                  }
  
          }
      }
  </code></pre>
  
  <p>//ForwardTo Method</p>
  
  <pre><code>public static void forwardTo(ServletContext ctx, HttpServletRequest req, HttpServletResponse resp, String dest)  throws ServletException
  {
      RequestDispatcher rd = ctx.getRequestDispatcher(dest);
      try
      {
          rd.forward(req, resp);
      }
      catch(Throwable t)
      {
          t.printStackTrace();
          throw new ServletException(t);
      }
  }
  </code></pre>
  
  <p>//HTML FORM</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="ISO-8859-1"&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form action = "UserServ" method="POST"&gt;
  &lt;h3&gt;Enter the details below to Sign In&lt;/h3&gt;&lt;br&gt;
  Email: &lt;input type="text" name="useremail" required&gt;&lt;br&gt;
  Password: &lt;input type="password" name="pass" required&gt;&lt;br&gt;
  &lt;input type="submit" value="Sign In"&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>