I am trying to invoke 2 different servlets from my HTML page based on the Submit button pressed. My html page -
<form method="POST" action="DownloadJar.do">Download at your own risk! <br> <center><input type="SUBMIT"></center>
<form method="POST" action="GetInitParams.do">Support Information just in case! <br><br> <center><input type="SUBMIT"></center>
And my web.xml - <servlet> <servlet-name>Download My Jar Now</servlet-name> <servlet-class>com.examples.web.downloadJar</servlet-class> </servlet> <servlet-mapping> <servlet-name>Download My Jar Now</servlet-name> <url-pattern>/DownloadJar.do</url-pattern> </servlet-mapping> <servlet> <servlet-name>Those people have asked for it this time</servlet-name> <servlet-class>com.examples.web.getInitParams</servlet-class> <init-param> <param-name>adminEmail</param-name> <param-value>admin@wickedlysmart.com</param-value> <param-name>mainEmail</param-name> <param-value>main@wickedlysmart.com</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Those people have asked for it this time</servlet-name> <url-pattern>/GetInitParams.do</url-pattern> </servlet-mapping>
Pressing either Submit button always invokes the first servlet. Are we allowed to have 2 Post requests on the same html page at all? Or is something else the matter.
You can, but you need to have a single form and use each button to change the form.action to the right value before submitting. Then everything should be OK.
Dave
Jenna Thomas
Greenhorn
Joined: Oct 03, 2005
Posts: 27
posted
0
Thanks for your answer Dave, but I cant find any sample code for that, how do I go about invoking 2 different servlets from the same form? Skeleton code would do. Thanks again!
Another approach, worth looking in to, is the "Front Controller" or "Command" pattern.
In your HTML form:
In your controller servelet:
By loading your actions into a Map, you can avoid the long if/else statement. I have a working example of this on my site: http://simple.souther.us See: SimpleCommand [ July 23, 2006: Message edited by: Ben Souther ]