For an HTML FORM with multiple SUBMIT buttons, how can a servlet respond differently for each button?

Richard Raszka

The servlet will respond differently for each button based on the html that you have placed in the HTML page. Let's explain.

For a submit button the HTML looks like <input type=submit name="Left" value="left">. A servlet could extract the value of this submit by using the getParameter("Left") from the HttpRequest object. It follows then that if you have HTML within a FORM that appears as:

<input type=submit name="Direction" value="left"><br>
<input type=submit name="Direction" value="right"><br>
<input type=submit name="Direction" value="up"><br>
<input type=submit name="Direction" value="down"><br>

Then the getParameter("Direction") from the HttpRequest would extract the value pressed by the user, either "left", "right", "up" or "down". A simple comparision in the servlet with the these values could occur and processing based on the submit button would be performed.

Similiarly,for submit buttons with different names on a page, each of these values could be extracted using the getParameter() call and acted on. However, in a situation where there are multiple buttons, common practice would be to use one name and multiple values to identify the button pressed.