6

I am working on a project in which there has to be a functionality to permit the users to update and delete certain rows of a table that will dynamically be displayed to them.

The user will click on a radio button to select which row he wants to update or delete and then will click in either the update or submit button.

According to his selection of update or delete, I have to pass the contents of the selected row to 2 a servlet. Now, the servlet for update is different from that of delete. I cannot mention the url pattern in the action attribute of the form as I need the values to be transferred to 2 different servlets according to the users choice.

Is it possible to achieve this?

Please suggest me a few solutions to this problem.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Lavanya Mohan
  • 1,496
  • 7
  • 28
  • 39
  • Why would you need two servlets? Why not just use one servlet, but dispatch to different handlers based on the button? That's a bit more in-line with servlet design. – g051051 Jul 10 '11 at 05:20
  • But if both update and delete are submit buttons, then is there a way to find out which button was hit? – Lavanya Mohan Jul 10 '11 at 06:10
  • I really dont have much knowledge about this. So it will be really appreciable if you could give some ideas – Lavanya Mohan Jul 10 '11 at 06:11

1 Answers1

21

The submit button 's name and value 's attribute will also be POSTED if you click that button to submit the form. In the servlet , you can check if you can get these parameters to know which button is clicked .

For example , suppose you have two buttons , one for update and one for delete

<input type="submit" name="update" value="Update Button">
<input type="submit" name="delete" value="Delete Button">

If the update button is clicked , it will post the variable update=Update Button If the delete button is clicked , it will post the variable delete=Delete Button

Then in the servlet :

    if (request.getParameter("update") != null) {
        //update button is clicked
        //Do the update action or forward the request to the servlet to do update action 

    } else if (request.getParameter("delete") != null) {
          //delete button is clicked
          //Do the delete action or forward the request to the servlet to do delete action
    }
Ken Chan
  • 84,777
  • 26
  • 143
  • 172
  • 1
    good answer but still more on this there: http://stackoverflow.com/a/11830483/2087666 – Remi Morin Aug 27 '13 at 14:19
  • 1
    Another good example here : [http://codingnoted.blogspot.com/2013/10/multiple-button-in-one-form-jsp-servlet.html](http://codingnoted.blogspot.com/2013/10/multiple-button-in-one-form-jsp-servlet.html) – shamcs Oct 20 '13 at 02:21