Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
shashanka jena
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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 ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • 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
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Rajesh Agarwal
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • 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 Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic