This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Buttons in same form hitting different servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Buttons in same form hitting different servlets" Watch "Buttons in same form hitting different servlets" New topic
Author

Buttons in same form hitting different servlets

Katie McCann
Ranch Hand

Joined: Jul 24, 2000
Posts: 45
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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Oct 21, 2000
Posts: 4340
    
    2

I dont know if it will work for you but what I did was use 2 forms on the same page(actually 3).


SCJP
Visit my download page
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
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

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
vaibhav punekar
Ranch Hand

Joined: Jan 20, 2001
Posts: 134
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.


VAIBHAV <BR>SCJP
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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: 4340
    
    2

I have also used the <input type=button onclick="../../sth"> approach
 
 
subject: Buttons in same form hitting different servlets
 
Similar Threads
do I have to use javascript to put multiple buttons on one JSP page ?
Resetting/overwriting a committed HttpServletResponseresponse object
Buttons in same form hitting different servlets
Two Submit buttons in JSP Page
button problem