This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes calling same servlet for two different button's action in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "calling same servlet for two different button Watch "calling same servlet for two different button New topic
Author

calling same servlet for two different button's action in jsp

shashanka jena
Greenhorn

Joined: Aug 23, 2006
Posts: 10
I have two button in a jsp page lets say "Add" and "Subtract" for add and subtraction operation respectively.For both the button can I wrirte a single servlet for doing the two differnet operations ?


Thanks and Regards<br />shashanka Jena
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Yes you can. You'll have to tell the servlet which action you wish to execute though. You could use a request parameter like "action=add", or "action=substract", depending on which button you press.


[My Blog]
All roads lead to JavaRanch
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by Satou kurinosuke:
Yes you can. You'll have to tell the servlet which action you wish to execute though. You could use a request parameter like "action=add", or "action=substract", depending on which button you press.


action attribute in form element specifies the servelet name.The question is to have a single servler.

You can do this by many ways, one I have given below:

<FORM ACTION="/cool/javaranch">
..
..
..
<INPUT TYPE=SUBMIT NAME="test" VALUE="ADD">
<INPUT TYPE=SUBMIT NAME="test" VALUE="SUB">
</FORM>


Query for VALUE in the servlet and proceed accordingly.

An Example


Rahul Bhattacharjee
LinkedIn - Blog
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10067
    
163

Originally posted by Rahul Bhattacharjee:
action attribute in form element specifies the servelet name.The question is to have a single servler.


I think Satou kurinosuke was mentioning the same approach which you have posted. By "action" Satou kurinosuke meant, the activity that has to be performed by the servlet.


[My Blog] [JavaRanch Journal]
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
I do not think shashanka jena wants two servlets.He needs a single servlet to handle both the type of requests.
Rajesh Agarwal
Ranch Hand

Joined: Aug 01, 2005
Posts: 79
Sashanka,

On click of these buttons you can call javascript methods and post to a different servlet from there.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Jaikiran, thank you.
Yes, that is what I was meaning. The name of "action" was confusing. I was not talking about the form's action, but about the a request parameter called action :
MyServlet?action=add
or
MyServlet?action=substract
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: calling same servlet for two different button's action in jsp