Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have two submit buttons in a jsp to two different controllers?

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.

like image 585
Lavanya Mohan Avatar asked Jul 10 '11 05:07

Lavanya Mohan


People also ask

Can you have two submit buttons?

yes, multiple submit buttons can include in the html form. One simple example is given below.

How do you handle multiple submit buttons?

Create another button with type submit. Also add a 'formaction' attribute to this button and give it the value of the secondary URL where you want to send the form-data when this button is clicked. The formaction attribute will override the action attribute of the form and send the data to your desired location.

How can use multiple submit button in PHP?

Having multiple submit buttons and handling them through PHP is just a matter of checking the the name of the button with the corresponding value of the button using conditional statements. In this article I'll use both elseif ladder and switch case statement in PHP to handle multiple submit buttons in a form.

How avoid multiple submit in JSP?

Disabling the button or displaying a process overlay are common methods to avoid multiple submits. If you desire to do this server side, think about if you would want the backend api to limit multiple POST requests?


1 Answers

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
    }
like image 119
Ken Chan Avatar answered Oct 11 '22 06:10

Ken Chan