Skip to content Skip to sidebar Skip to footer

How To Find Out Which Html Button Was Pushed In My Servlet?

I am creating a registration form which contains two submit buttons. I need to know which button is clicked in the form in my servlet code?

Solution 1:

Read the answers to this question.

So, in

Stringbutton1= request.getParameter("button1");
Stringbutton2= request.getParameter("button2");

the value which isn't null is the pressed button.

Or, if you want to use the same name for the two buttons you can set a different value

<inputtype="submit" name="act" value="delete"/>
<inputtype="submit" name="act" value="update"/>

Then

String act = request.getParameter("act");
if (act == null) {
    //no button has been selected
} elseif (act.equals("delete")) {
    //delete button was pressed
} elseif (act.equals("update")) {
    //update button was pressed
} else {
    //someone has altered the HTML and sent a different value!
}

Solution 2:

Only the clicked button will be a successful control.

<inputtype="submit" name="action" value="Something">
<inputtype="submit" name="action" value="Something Else">

Then, server side, check the value of the action data.

Solution 3:

Use This Code...

In JSP File...

<form action="MyServ">
            <inputtype="submit" name="btn1" value="OK">
            <inputtype="submit" name="btn2" value="OK">
        </form>

In Servlet File..

if (request.getParameter("btn1") != null){
       // do something
 }
elseif (request.getParameter("btn2") != null){
       // do something
 }

Solution 4:

You can add a hidden field to the form and when a user clicks a button set its value to "btn1" or "btn2" using javascript before sumbit.

Cheers :)

Post a Comment for "How To Find Out Which Html Button Was Pushed In My Servlet?"