aspose file tools*
The moose likes Servlets and the fly likes Two Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Two "Post"s on the same html page valid?" Watch "Two "Post"s on the same html page valid?" New topic
Author

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

Jenna Thomas
Greenhorn

Joined: Oct 03, 2005
Posts: 27
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!


Cheers!
Jenn SCJP1.5
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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
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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Oct 03, 2005
Posts: 27
Thanks David, it worked!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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 ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jenna Thomas
Greenhorn

Joined: Oct 03, 2005
Posts: 27
Thanks Bear.

Will check this approach Ben. Thanks for your time!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Two "Post"s on the same html page valid?