0

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
trixrabbit
  • 259
  • 7
  • 22

1 Answers1

2

How to create a button in a JSP file that calls a Java method

Use ajax. See also How to use Servlets and Ajax?


however, my search disapear since URL no longer contains ?userName=foo

Simply let the form submit to the desired URL.

<c:url value="/GroupVerification" var="groupVerificationURL">
    <c:param name="userName" value="${param.userName}" />
</c:url>
...
<form method="post" action="${groupVerificationURL}">
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I was wondering if this was possible without javascript ? I tried to submit the URL but I get 2 problems : 1) the project name repeat itself in the url http://localhost:8080/VerificationAD/VerificationAD/... 2) the parameter special characters don't get converted to % form – trixrabbit May 27 '15 at 19:29
  • Without JS? Just use HTML, but you already know how to do that. As to double context path, that's strange. Perhaps you modified the answer to manually include the context path? You shouldn't do that. The `` already auto-includes it. As to URL encoding of params, that's strange. The `` already auto-encodes it. Or are you talking about whatever you see in browser's address bar rather than in actual HTML source? – BalusC May 27 '15 at 20:03
  • 1) The double context was my mistake, I had written :
    ">, the
    – trixrabbit May 28 '15 at 12:44
  • The `%20` is only for spaces in URI path and the `+` is correct for spaces in query string. So whoever is responsible for inserting `%20` in query string is doing it wrong. The server side should however have no problem with `+` when parsing query string parameters. So it's hard to understand your "doesn't work" statement. Perhaps you're manually fiddling with `getQueryString()` instead of using `getParameter()` or so? – BalusC May 28 '15 at 12:50
  • I use request.getParameter in my doGet() to get the groupName, then I just create a form (groupVerificationForm) ,a bean (group) and I set the bean with the functions in the form. I then request.setAttributes with my group. I then display the elements in my jsp. The doPost only contain the code in this thread, I use doPost only to call a function and I try to stay on the same page as I were. – trixrabbit May 28 '15 at 14:09