Which submit button was pressed?
I have a form with several submit button that go to the same servlet. To define the next JSP to redirect I need to know which button was pressed. In ASP i'll use button.x as a parameter name. I tried both .x and .X - the servlet doesn't recognize neither one. What is the correct way to do this?
[311 byte] By [
natanfw] at [2007-9-26 2:34:18]

Natanfw,
how are you processing your form, is it through jsp pages or through beans? anyhow, you can put the same name attribute in your submit buttons and get the button values via the request.getParameter("buttonName") method. the return value of your request.getParameter() method is actually the value of the button that you clicked.
from their you make an if loop using the StringObject.equals("buttonValueHere") method.
for example:
your form here:
<form method="" action="hello.jsp">
<input type="submit" value="hello" name="myButton"/>
<input type="submit" value="hi" name="myButton"/>
</form>
your hello.jsp here:
String buttonValue = request.getParameter("myButton");
if (buttonValue.equals("hello")) {
//your code here
} else if (buttonValue.equals("hi")) {
//your code here
}
if you will be using a bean, you can get the button value clicked via the setMyButton and getMyButton() methods. This will be another story though.
i hope this one helps!
Cris