• 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

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

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 10445
227
IntelliJ 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 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.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not think shashanka jena wants two servlets.He needs a single servlet to handle both the type of requests.
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sashanka,

On click of these buttons you can call javascript methods and post to a different servlet from there.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
New rule: no elephants at the chess tournament. Tiny ads are still okay.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic