Hi. I have an HTML form with 2 different buttons. I would like each button to hit a different servlet. But of course, I only have 1 form tag with 1 "action" attribute. Is there a way that this can be accomplished? I realize that I could just use 1 servlet, and have some logic to read the name of the button that was hit. But I'd rather avoid having that extra if...else logic. Thanks!
In Javascript, the form.action variable is writable, so you can do this: (note this off the top of my head)
I think this is more or less correct. Note that personally I use type=button, not type=submit since this can possibly cause the request to be sent twice (or so it seemed) Dave
I dont know if it will work for you but what I did was use 2 forms on the same page(actually 3).
I never took notes in college. That's how I got a 4.0 the first 2 years, and a 3.5 the second two years.
Madhav Lakkapragada
Ranch Hand
Joined: Jun 03, 2000
Posts: 5040
posted
0
I realize that I could just use 1 servlet, and have some logic to read the name of the button that was hit. But I'd rather avoid having that extra if...else logic. I don't know the disadv's of this, but I personally prefer this approach. Not that there is anything wrong with the javascript approach or anything, I am ignorant on that topic. After all Servlets are used as Controllers in one of the famous (MVC) patterns, right! Just to throw another spin to the problem can we link the buttons to different JSP's maybe. I should think abt it... regds. - satya
Hi, 1)way is to use <img src="">tag. Create two different images looking like buttons with name. In your form use img src tag and give different URLs for those. This will definitely help.Also you ll avoid using Javascript which is browser inocmpatible in many cases. 2)Other way is to use <input type=button onclick="../../sth"> 3)If one of your button is "Back" and you wish to give back page URl,try instead onclick="history.back()".In that case your servlet will have only one action. I hope this will help.
Originally posted by Randall Twede: I dont know if it will work for you but what I did was use 2 forms on the same page(actually 3).
I've used this solution too, but you can't have the buttons next to each other (the forms usually enforce some formatting) and they can't share other input elements like drop-downs.
Randall Twede
Ranch Hand
Joined: Oct 21, 2000
Posts: 3901
posted
0
I have also used the <input type=button onclick="../../sth"> approach
subject: Buttons in same form hitting different servlets