title: Two submit buttons in a JSP without multiple forms, same controller
body: <p>I have read the similar topics <a href="https://stackoverflow.com/questions/2025280/have-multiple-submit-buttons-in-a-form-and-determine-which-was-pressed-in-a-cont">like this</a>, and <a href="https://stackoverflow.com/questions/7938302/processing-multiple-submit-buttons-in-jsp">that one</a> but it did not help with my problem.</p>
  
  <p>I created a simple JavaEE mvc web app. The jsp page contains a form with two text fields and two buttons. The first text field to enter an Id, the second to enter a name. Depending on which button is clicked a Servlet routes to the appropriate method (Search by ID or Search by Name).
  Search by id method works correctly. And I see the following path in the address bar: <code>http://localhost:8080/employees_war_exploded/ControllerServlet?textEmployeeId=1&amp;command=Search_ID&amp;employeeName=</code>  </p>
  
  <p>However there is a problem with a Search by name method. It does not show any results. Here is what I see in the address bar: <code>http://localhost:8080/employees_war_exploded/ControllerServlet?textEmployeeId=&amp;employeeName=ann&amp;command=Search_Name</code></p>
  
  <p>I guess the problem is that in both cases it gets the parameters of both text fields ("textEmployeeId" and "employeeName"). How can I make it process both inputs separately in the same form? Maybe there is some other reason for the problem that I do not see?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;form class="form-style" name="form1"&gt;
    &lt;label&gt;ID:
      &lt;input type="text" name="textEmployeeId" value="${tempEmployee.id}" /&gt;
    &lt;/label&gt;
    &lt;input type="submit" name="command" value="Search_ID"&gt;
    &lt;br&gt;
    &lt;br&gt;
  
    &lt;label&gt;Name:
      &lt;input type="text" name="employeeName" value="${tempEmployee.name}" /&gt;
    &lt;/label&gt;
    &lt;input type="submit" name="command" value="Search_Name"&gt;
    &lt;br&gt;
    &lt;br&gt;
  &lt;/form&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>ControllerServlet.java</strong></p>
  
  <pre><code>    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          try {
              String theCommand = request.getParameter("command");
  
              if (theCommand == null) {
                  theCommand = "EMPLOYEE_LIST";
              }
  
              // route to the appropriate method
              switch (theCommand) {
                  case "EMPLOYEE_LIST":
                      listEmployees(request, response);
                      break;   
                  case "Search_ID":
                      searchById(request, response);
                      break;
                  case "Search_Name":
                      searchByName(request, response);
                      break;
                  default:
                      listEmployees(request, response);
              }
  
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  private void searchByName(HttpServletRequest request, HttpServletResponse response) throws Exception {
          String nameString = request.getParameter("employeeName");
  
          List&lt;Employee&gt; namedEmployees = employeeDbUtil.searchEmployees(nameString);
          request.setAttribute("EMPLOYEES", namedEmployees);
  
          RequestDispatcher dispatcher = request.getRequestDispatcher("/search-by-name.jsp");
          dispatcher.forward(request, response);
  
  
      }
  
  
   private void searchById(HttpServletRequest request, HttpServletResponse response) throws Exception {
  
              String textString = request.getParameter("textEmployeeId");
  
               Employee theEmployee = employeeDbUtil.getEmployeeById(textString);
                  request.setAttribute("THE_EMPLOYEE", theEmployee);
  
                  RequestDispatcher dispatcher = request.getRequestDispatcher("/show-employee.jsp");
                  dispatcher.forward(request, response);
              }
  
          }
  </code></pre>
  
  <p>Update: I'm getting a NullPointerException for the searchByName method. Could there be a problem with this method from a DAO class that is called from ControllerServlet?</p>
  
  <pre><code>public List&lt;Employee&gt; searchEmployees(String employeeName) throws Exception {
      List&lt;Employee&gt; employeeList = new ArrayList();
  
      Connection myConnection = null;
      PreparedStatement preparedStatement = null;
      ResultSet resultSet = null;
  
      try {
          employeeName += "%";
          preparedStatement = myConnection.prepareStatement("SELECT * FROM employees WHERE LOWER(name) like LOWER(?)");
          preparedStatement.setString(1, employeeName);
          resultSet = preparedStatement.executeQuery();
          while (resultSet.next()) {
              Employee tempEmployee = resultSetToEmployee(resultSet);
              employeeList.add(tempEmployee);
          }
          return employeeList;
      }
      finally {
          DbExceptions.close(resultSet);
          DbExceptions.close(preparedStatement);
          DbExceptions.close(myConnection);
      }
  
  }
  </code></pre>