• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Buttons in same form hitting different servlets

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have also used the <input type=button onclick="../../sth"> approach
reply
    Bookmark Topic Watch Topic
  • New Topic