• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Identifying which button was clicked

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have 4 submit buttons in my jsp page and on click of each button i generate different report.on submiting i want to pass a value for each report to a single servlet.and there i will check that if value of parameter is this then call that method i this then that.

How can i do this ???

Please help..
Thanks
[ November 16, 2008: Message edited by: Bear Bibeault ]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can try specifying your servlet path in the action attribute of the form tag.




Hope that helps
 
Nikki Agr
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, here's how you can retrieve the value in your Servlet. Use



where name is the name you give to the control in your form.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can use java script to submit the form where you can attach a parameter with the action and retrieve it in the servlet

OR


you can have four different servlet-mapping for the same servlet and use java script to submit to each of those mappings depending on the button being clicked.

In this way you can use getRequestURI() on the request object to know from which url(specified in the deployment descriptor) the request came from and call the appropriate method in your servlet
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On click of the button you can attach a key as request parameter and then generate the report according to the key you have passed.

One way you can do this is noted down.

<script>
getReport= function(formObj,key)
{
formObj.key.value=key;
formObj.submit();
}
</script>

<form method="get" action="myServlet" &>
<input type="text" name="key"/>
<input type="button" value="1" onklick="getReport(this.parentNode,1)" />
<input type="button" value="2" onklick="getReport(this.parentNode,2)" />
<input type="button" value="3" onklick="getReport(this.parentNode,3)" />
<input type="button" value="4" onklick="getReport(this.parentNode,4)" />
</form>


Now you get the key in the servlet using request.getParameter("key") and generate report according to it.
 
Girish Gopal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry the < symbol got screwed up in the last post..posting it again..

On click of the button you can attach a key as request parameter and then generate the report according to the key you have passed.

One way you can do this is noted down.

< script>
getReport= function(formObj,key)
{
formObj.key.value=key;
formObj.submit();
}
< /script>

< form method="get" action="myServlet" &>
< input type="text" name="key"/>
< input type="button" value="1" onklick="getReport(this.parentNode,1)" />
< input type="button" value="2" onklick="getReport(this.parentNode,2)" />
< input type="button" value="3" onklick="getReport(this.parentNode,3)" />
< input type="button" value="4" onklick="getReport(this.parentNode,4)" />
< /form>


Now you get the key in the servlet using request.getParameter("key") and generate report according to it.
 
Sheriff
Posts: 67651
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But why go through all that JavaScript when it's not necessary? Simply assign a name and value to the submit buttons and the request parameter will be submitted.

[ November 16, 2008: Message edited by: Bear Bibeault ]
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way to do this is to have four radio buttons with the same name for different kinds of reports, and one submit button.
When one of the radio buttons will be selected on a form, that value will be sent to a servlet whose path should be specified in the action attribute of the form.
In the servlet the value can be retrieved using the request.getParameter(name of the radio button)method.
 
Bear Bibeault
Sheriff
Posts: 67651
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's really no need to needlessly complicate the form with extra radio buttons. Again, it's as simple as assigning values to the submit buttons.
[ November 16, 2008: Message edited by: Bear Bibeault ]
 
Girish Gopal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool, I never knew that button elements value will be added to request parameter on form submit.Thanks Bear that was a simple solution.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic