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]
# 1

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

pulat85 at 2007-6-29 9:58:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Thanks for the respond. we found an easy way to define how the form elements will be passed. Change post method to get and use any html url as a source. It will show all parameters in the url. In particular it showed that the button is sent as button_name.x. So if your button name is B1 you should retrieve "B1.x" parameter in the following JSP or Servlet.

Thanks

natanfw at 2007-6-29 9:58:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...