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.
I'm using MVC design, the application search ldap (AD) and returns the users that are member of a group.
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).
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:
JSP :
<!-- Send the userName input to the servlet to perform the search-->
<form method="get" action="<c:url value="/GroupVerification"/>">
<table>
<tr>
<td><label for="userCip">Search by CIP : </label></td>
<td><input type="text" id="userCip" name="userCip" /></td>
<td><input type="submit" value="Search" /></td>
</tr>
</table>
</form>
...<!-- html code for displaying results -->
<!-- Button to create Excel file with results-->
<form method="post" action="<c:url value="/GroupVerification"/>">
<input type="submit" name="excelExport" value="Export in Excel" />
</form>
Servlet :
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 {
...
<!-- Code to send the userName to the form to perform search and setAttributes -->
this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
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...
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?