title: How to create a button in a JSP file that calls a Java method
body: <p>I'm building a Java EE application (still learning) and I don't know how to simply associate a button to a function following good practice.</p>
  
  <p>I'm using MVC design, the application search ldap (AD) and returns the users that are member of a group.</p>
  
  <p>I use GET form to send the user name to my servlet, which send it to a form to do the search and then I return the information to my vue. (I chose GET over POST because some user like to directly fill the ?userName= in the URL).</p>
  
  <p>I created a function that export the results into a excel file and I want to associate a button to that function. With what I know here is what I did so far:</p>
  
  <p>JSP :</p>
  
  <pre><code>&lt;!-- Send the userName input to the servlet to perform the search--&gt;
  &lt;form method="get" action="&lt;c:url value="/GroupVerification"/&gt;"&gt;    
          &lt;table&gt;
              &lt;tr&gt;
                  &lt;td&gt;&lt;label for="userCip"&gt;Search by CIP : &lt;/label&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;input type="text" id="userCip" name="userCip"  /&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;input type="submit" value="Search"  /&gt;&lt;/td&gt;
              &lt;/tr&gt;
  
          &lt;/table&gt;
  &lt;/form&gt;
  
  ...&lt;!-- html code for displaying results --&gt;
  
  &lt;!-- Button to create Excel file with results--&gt;
  &lt;form method="post" action="&lt;c:url value="/GroupVerification"/&gt;"&gt;
       &lt;input type="submit" name="excelExport" value="Export in Excel" /&gt;                                   
  &lt;/form&gt;
  </code></pre>
  
  <p>Servlet : </p>
  
  <pre><code>public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
  
      if (request.getParameter("excelExport") != null) {
          GroupVerificationForm.excelExport(user.getGroupList());            
      }
  
      this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
  
  
  public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
       ...
      &lt;!-- Code to send the userName to the form to perform search and setAttributes --&gt;
  
      this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
  </code></pre>
  
  <p>I'm not sure if this is bad practice but user is a private global variable in my servlet which is associated to a User in the doGet() method when a search is made...</p>
  
  <p>Anyway, when I click the "Export to excel" button, the doPost() is called and my results are exported to excel, however, my search disapear since URL no longer contains ?userName=foo. My problem is that I would like to remain on the same page. Is there a way to only call a method with a button without calling doPost so I don't lose my URL ? Or should I copy the doGet code in my doPost so the search is re-made and re-displayed?</p>