1

I am pretty new in JSP and Servlet development and I have to do the following thing:

I have a JSP page that contains a JQuery button element.

<th width = "8.33%">
  <form action="salwf.do" method="post">
    <button name="status" value="Accept" class="acceptButton">ACCEPT BUTTON</button>
    <button name="status" value="Cancel" class="cancelButton">CANCEL BUTTON</button>
    <button name="status" value="SAP" class="sapButton">SAP ICON BUTTON</button>
  </form>
</th>

As you can see I put this button element into a form. Each button have a property named status for all buttons and a specific value for each button (Accept for the first button, Cancel for the second button and Sap for the third button).

Ok, now I want to submit this form to the Servlet that handle the page that contains the form. This page have the following URL:

http://localhost:7001/edi-mon/salwf.do

and this is the servlet configuration into the web.xml file (Servlet definition and Servlet URL mapping):

<servlet>
    <servlet-name>salwf</servlet-name>
    <servlet-class>it.sistinf.ediweb.monitor.servlets.Salwf</servlet-class>
    <load-on-startup>0</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>salwf</servlet-name>
    <url-pattern>/salwf.do</url-pattern>
</servlet-mapping>

Then I have the Salwf class that implement the HttpServlet interface and that handle this JSP page. So this Servlet have also to handle the post of the previous form, to do this I have implement the doPost() method inside this class, in this way:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
                      throws ServletException, IOException {
    String status = request.getParameter("status");
}

So I hopped to retrieve the value of the status name of the JQuery clicked button but it didn't work.

When debugging the application I tried to click on the buttons but I never entered into the doPost() method and so I can't retrieve the value of the status request paramether.

Why? What am I missing? How can I fix this issue?

M. A. Kishawy
  • 5,001
  • 11
  • 47
  • 72
  • 1
    how do you submit the form ? I don't see a SUBMIT control there, so you must be using js. Please add the click function. – BigMike Dec 01 '14 at 13:49
  • @BigMike what exactly mean? The form is not automatically submitted whe one of the 3 buttons are clicked? Can you post me a code snippet to solve my issue? –  Dec 01 '14 at 13:54
  • 2
    @AndreaNobili, surely, not. To sumbint form you have to create `` element with type `submit` or call `submit()` method of form using javascript. – AlexR Dec 01 '14 at 13:55
  • BTW all your buttons have the same name. I am not sure this is what you really want. – AlexR Dec 01 '14 at 13:56
  • @AlexR, IE will treat a `button` with no `type` as a `submit` - so he could actually be seeing it submit if that's where he is testing – josh.trow Dec 01 '14 at 13:56
  • @josh.trow does it works this way also on all other browsers ? Just working on IE usually doesn't mean it's a good thing to do. – BigMike Dec 01 '14 at 14:11
  • @BigMike I absolutely agreed it's a bad way to do it, and apparently I was remembering wrong (IE is the only one to NOT always do that...) http://stackoverflow.com/a/9643866/446747 – josh.trow Dec 01 '14 at 14:14
  • tested right now with IE 11 and it works too, guess I've learned something new today! – BigMike Dec 01 '14 at 14:26
  • You're expecting to retrieve the button name as a request parameter? Or how else are you confirming the servlet isn't being hit? Add a log statement to your `doPost` to confirm – kolossus Dec 02 '14 at 14:35

1 Answers1

0

This should work, no jQuery involved. Kinda in a hurry so can't be too polished on code.

<th width = "8.33%">
  <form action="salwf.do" method="post" name='theForm'>
    <button value="Accept" class="acceptButton" onclick='setVal("accept")'>ACCEPT BUTTON</button>
    <button value="Cancel" class="cancelButton" onclick='setVal("cancel")'>CANCEL BUTTON</button>
    <button value="SAP" class="sapButton" onclick='setVal("sap")'>SAP ICON BUTTON</button>
    <input type='hidden' name='status'>
  </form>
</th>
<script>
function setVal(buttonCmd) {
   document.forms['theForm'].status.value = buttonCmd;
   document.forms['theForm'].submit();
   return false;
}

</script>

How does it works: it binds a js function on each button's click event, when the button is pressed, the value of the hidden input named "status" is set to a value depending on the button itself and the form is submitted.

There are probably more elegant and cleaner ways, but this is the easiest coming in my mind without entering too deep in your HTML structure.

BigMike
  • 6,683
  • 1
  • 23
  • 24
  • mmm I understand your comment but it still can't enter into the doPost() method of my servlet –  Dec 01 '14 at 14:17
  • have you changed also your HTML to match mine? Try putting the entire url to your servlet (usually http://localhost:/webappname/servletMapping ) – BigMike Dec 01 '14 at 14:19
  • Imppossibile amico mio. Check if the webapp is correctly deployed. Buona fortuna. – BigMike Dec 01 '14 at 14:31