File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes how to detect which button is clicked in a FORM from server side ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to detect which button is clicked in a FORM from server side ?" Watch "how to detect which button is clicked in a FORM from server side ?" New topic
Author

how to detect which button is clicked in a FORM from server side ?

Artemesia Lakener
Ranch Hand

Joined: Jun 21, 2005
Posts: 162
originally posted a thread asking "do I need to use javascript to put multiple buttons on a form ?" many people said yes and it was moved to "HTML" section. However, I read one response thread and it says it can also be handled by the server side using servlet. I don't know how. Could someone tell me how to use servlet to find out which action button is clicked and how to pass different action names ? any short sample will be great.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
You can use a hidden field for this purpose. Just assign a unique value to every button and pass that value to that hidden field, on button click. Check that hidden/parameter on your server and you will know which button was actually pressed.

thanks.
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

You should get button name value. If it's image button, then you should get button name dot x and y coordinates.


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

There are several methods.

One is to give all of your buttons the same name.
If you do so, only the one clicked will be sent to the server.

Then in your servlet code, you can read parameter and branch on it's value.


The problem with this approach is that your controller logic is dependent on your view (look and feel). At some point a client, manager, or web designer will want the buttons to say "save" instead of "enter". At this point, you will need to change your logic in the servlet to match the value of the buttons.

For this reason, I prefer to give each button a separate name and test to see which is not null in the servlet.


Then in the servlet.



Now management can change the value of the buttons till their heart's content without breaking your servlet code.

The third option, which D Rog mentioned, is to use input fields of type "image". The visible part of an image tag is the image itself or whatever is in your "alt" attribute if the image can't be found. If you're using images, you can give them all the same name and read the value like in the first example. The value never gets printed to the page so there is no need for anyone to change it.
[ August 03, 2005: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

Yet another option is to not switch on the server side at all. If you like to keep the server-side code as UI-unbound as possible, I'd have each button submit to a completely different action rather than having a single action that needs to act as a traffic cop depending upon the button name.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to detect which button is clicked in a FORM from server side ?