Hello all, I am pretty new to JSP because I am an ASP.NET developer by day so pardon me if this is an elementary question. How does JSP know which button was clicked when you have more than one button within a form?
I have the following buttons within the same form:
The value of the "command" request parameter will reflect the value of the button that is clicked to submit the form. If this is not what you are seeing in your bean, check the URL of the submission (if using GET) to see if it is correct there, indicating that the bean has a coding issue.
If using POST you'll need to inspect the request body to see the submission parameters (or temporarily switch to GET). [ February 14, 2008: Message edited by: Bear Bibeault ]
As Bear mentioned, responding to requests with JSPs is no longer considered a best practice when developing with server side Java.
That being said... An unclicked button will send no parameters to the server. So, if you have two submit buttons in a form, the one that has been clicked will send a parameter, the other won't.
If you're bean has properties for each button, you can determine which was clicked by testing to see which is not null.
Also, if you give several submit buttons the same name but different values, you can determine which was pressed by reading the value sent to the server. [ February 14, 2008: Message edited by: Ben Souther ]
I would not suggest using such code. Not only should such code not be in a JSP to begin with, it implicitly assumes that anything that's not a change operation, is a delete. That's not a good practice.
One reason I don't branch on the value of a button (in the JSP, controller, or model) is that marketing and branding type people seem to love to change the text in buttons. As soon as you write it with the text "Enter", someone will feel the urge to have it changed to "Submit". A year later, someone else will want it to say "Save".
For this reason, I think it's better to branch on whether the button name is present in the form submission and ignore the actual value.