• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Buttons in same form hitting different servlets

 
Katie McCann
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont know if it will work for you but what I did was use 2 forms on the same page(actually 3).
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
vaibhav punekar
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have also used the <input type=button onclick="../../sth"> approach
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic