I write a web service using servlet, freemarker, jetty, hibernate, mysql.

There are two desired post requests: " Add source " and " Delete source "

But there is a hitch, I explain along the way.

There is such a form

<form action="/admin" method="post"> <div class="mdl-textfield mdl-js-textfield"> <input class="mdl-textfield__input" type="text" id="sample1" name="sourceIp"> <label class="mdl-textfield__label" for="sample1">IP...</label> </div> <div class="mdl-dialog__actions mdl-dialog__actions--full-width"> <button type="submit" class="mdl-button" value="Add" ">Add source</button> <button type="submit" class="mdl-button" value="Delete">Delete source</button> <button type="button" class="mdl-button close">Disagree</button> </div> </form> 

It has two buttons that are responsible for sending the form.

But each of them refers to the doPost() method in the servlet and, accordingly, the same operations are performed.

In order to get around this, I decided that it was worth making a selection by the attribute of the pressed button.

Those. click on the "Add ..." request in the servlet goes to the doPost() method and in it if I make a selection. If the user clicks on the buttons with the attribute value=Add then I do some operations with the entered data, otherwise others.

The question is, how do I get the attribute of the button the user clicked on? There is a getAttribute("value"); method getAttribute("value"); but I cannot understand which attribute of all it takes and how it works (

    1 answer 1

    HTML:

     <div class="mdl-dialog__actions mdl-dialog__actions--full-width"> <button type="submit" class="mdl-button" name="clickAdd" value="Add">Add source</button> <button type="submit" class="mdl-button" name="clickDelete" value="Delete">Delete source</button> <button type="button" class="mdl-button close" name="clickDisagree" value="Disagree">Disagree</button> </div> 

    Inside the servlet:

     ... if(request.getParameter(clickAdd)!=null) System.out.println("Ты кликнул ADD"); else if(request.getParameter(clickDelete)!=null) System.out.println("Ты кликнул Delete"); ... 
    • Thanks, helped) - Vorobey.A