• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Delegating requestes to different servlets based on the submission.

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have a requirement here. I have an ordinary HTML form with some details like customer information and with three submit buttons. My requirement is, I need to send the same customer information to three different servlets individually based on which Submit button is clicked. How do we accomplish this. what needs to be mentioned in ACTION parameter in FORM element. I'm not sure if I have made my question clear . Also is there anything like Servlet Chaining like we have for filter Chaining. If so, do we mention that in Deployment Descriptor.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The equivalent to servlet chaining can be accomplished with the RequestDispatcher.
And no the chain doesn't have to be declared in your deployment descriptor.

It sounds like the pattern you're looking for is the FrontController.
I have a simple implementation of this on my site:
http://simple.souther.us
Look for SimpleCommand.

Bear Bibeault has published a minimal framework based on MVC and the FrontController patterns. If you search the BlatantAdvertising forum for "FrontMan" you will find a link for downloading it.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could either handle this in the browser (by having each submit button use JavaScript to set the action attribute before submitting the form), or on the server (by having a servlet that dispatches to one of the 3 others based on the name attribute of the submit button),
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

For Ulf's message

You could either handle this in the browser (by having each submit button use JavaScript to set the action attribute before submitting the form), or on the server (by having a servlet that dispatches to one of the 3 others based on the name attribute of the submit button),



How do we decide which servlet to be invoked used action event for submit button using JavaScript. Some where I heard that AJAX is used for these kinds of purposes.
Also how do we get the attribute of the submit button using the request object? who would set the attribute for the request, so that it can be retrieved at the later point? My question sounds very lame to me
. Also is there any way that servlet can know which actual button is clicked on the client machine.


For Ben's message,

I have gone through the link and also through the sevlet "ControllerServlet.java" . There is one part of the program I could not understand. In the below method, I didn't understand getting parameter using request.getParameter("form_action"). What is form_action referring to here. Where is this taken from?

public void processCommand(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException{

String formAction = request.getParameter("form_action"); if(null == formAction){
formAction = "init";
}

Also in the web.xml,
servlet mapping is done *.do, does this mean any name with extension "do" would invoke the servlet "ControllerServlet". I mean any irrelevant name say "http://localhost:8080/SimpleCommand/javaranch.do" also invokes "ControllerServlet". Please let me know, if my understanding is correct.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forgot to thank you for the information, and would be waiting for your response to the extension of my query.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How do we decide which servlet to be invoked used action event for submit button using JavaScript. Some where I heard that AJAX is used for these kinds of purposes.


It has nothing to do with AJAX. If one of the buttons is clicked (and before the form is submitted), the onKlick handler can change the "action" attribute of the form to whatever is appropriate for that button.

Also how do we get the attribute of the submit button using the request object? who would set the attribute for the request, so that it can be retrieved at the later point?
Also is there any way that servlet can know which actual button is clicked on the client machine.


Both of these seem to be the same question. Submit buttons can have a name attribute, and as such they are form parameters that get submitted to the server. If you give the buttons different names, you can find out on the server which one was clicked.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chandra Kota:
I didn't understand getting parameter using request.getParameter("form_action"). What is form_action referring to here. Where is this taken from?



In the example app, 'form_action' is a form field (a hidden field in the HTML form). In your case, if you give all three of your buttons the same name, you can determine which one was pressed by using request.getParameter.
You can then dispatch to the proper servlet based on that value.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Also in the web.xml,
servlet mapping is done *.do, does this mean any name with extension "do" would invoke the servlet "ControllerServlet". I mean any irrelevant name say "http://localhost:8080/SimpleCommand/javaranch.do" also invokes "ControllerServlet". Please let me know, if my understanding is correct.[/QB]



Apps build around the command pattern or front controller pattern often have only one servlet. That servlet does nothing more than delegate to a bunch of command objects. In my example I used a form field to determine which object to use. I did this for simplicity's sake. A more common approach is to parse any command URL (in this case any URL ending with *.do) to find out which command to use. This way one servlet (and thus one servlet mapping) can handle all of the URLs (or form actions) in your application.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to you all. Now my doubt is clarified.
 
Water proof donuts! Eat them while reading this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic