Sourav Roy
Sourav Roy

Reputation: 73

Button calling different servlet code in html form

I am bit new to Java web programming I have two buttons(Test connection and Execute) in an html form and the form action is a servlet. How can I differentiate the action in the servlet based on which button is clicked.

Thanks

Upvotes: 0

Views: 65

Answers (1)

developerwjk
developerwjk

Reputation: 8659

In the HTML:

<input type='submit' name='submitButton' value='Test connection' />
<input type='submit' name='submitButton' value='execute' />

In the servlet:

String clickedButtonValue = request.getParameter("submitButton");
if("Test connection".equals(clickedButtonValue))
{
 ...
}
else if("execute".equals(clickedButtonValue))
{
 ...
}
else ...

The reason this works: With both submit buttons sharing the same name attribute, the browser will only send the value of the one that was clicked.

Upvotes: 1

Related Questions