My Java file is:
public class MyClass {
public void method1() {
// some code
}
public void method2() {
//some code
}
public void method3() {
//some code
}
}
In my JSP page I have three HTML buttons.
If I click on button1, then only method1 will be called, if I click on button2 then only method2 will execute, and if button3, then only method3, and so on.
How can I achieve this?
Just give the individual button elements a unique name. When pressed, the button’s name is available as a request parameter the usual way like as with input elements.
You only need to make sure that the button inputs have
type="submit"as in<input type="submit">and<button type="submit">and nottype="button", which only renders a “dead” button purely foronclickstuff and all.E.g.
with
Alternatively, use
<button type="submit">instead of<input type="submit">, then you can give them all the same name, but an unique value. The value of the<button>won’t be used as label, you can just specify that yourself as child.E.g.
with
See also: