*
The moose likes Servlets and the fly likes Delegating requestes to different servlets based on the submission. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Delegating requestes to different servlets based on the submission." Watch "Delegating requestes to different servlets based on the submission." New topic
Author

Delegating requestes to different servlets based on the submission.

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41116
    
  45
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),


Ping & DNS - my free Android networking tools app
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Nov 22, 2008
Posts: 18944
Forgot to thank you for the information, and would be waiting for your response to the extension of my query.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41116
    
  45
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Dec 11, 2004
Posts: 13410


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

Joined: Nov 22, 2008
Posts: 18944
Thanks to you all. Now my doubt is clarified.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Delegating requestes to different servlets based on the submission.
 
Similar Threads
manage get and post?
form action
Capturing Servlet Output
Jr. & Sr. Java Developers needed in FL!!!!!!
How to index the JSP page for different pages