Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two "Post"s on the same html page valid?

 
Jenna Thomas
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlets newbie alert.

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.

Thanks!
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well firstly the simple answer may be to create valid HTML - you haven't closed your tags. Therefore it could be as simple as closing the first form:


[ July 23, 2006: Message edited by: David O'Meara ]
 
Jenna Thomas
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David, it worked!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with the code in your first point is that you have the forms embedded in each other (no </form> for the first form). That cannot be.

But you can have as many indepenendent forms with disparate actions on your page as you would like.
 
Jenna Thomas
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear.

Will check this approach Ben. Thanks for your time!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic