wood burning stoves*
The moose likes Servlets and the fly likes Multiple Forms in Servlet/JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Multiple Forms in Servlet/JSP" Watch "Multiple Forms in Servlet/JSP" New topic
Author

Multiple Forms in Servlet/JSP

Stevie Shorey
Ranch Hand

Joined: Dec 10, 2012
Posts: 45

Hi, I am using multiple forms in a JSP page (toggling with javascript).
I want to know if there is any way to have all three forms use one servlet. i.e. how can i identify which form is sending data from within the doPost() of the servlet apart from checking each getParameter() for null values (a long and tedious option!)
Thanks,
Stevie
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61448
    
  67

Easiest: include a hidden input in each form that identifies the source. Ick.

Better: use a unique mapping for each variant. Meh.

Best: re-evaluate why you want to do this in the first place. If there are different actions that need to take place depending upon which form is submitted, why mash it all together? Sounds like a bad move to me.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stevie Shorey
Ranch Hand

Joined: Dec 10, 2012
Posts: 45

Bear Bibeault wrote:Easiest: include a hidden input in each form that identifies the source. Ick.

Better: use a unique mapping for each variant. Meh.

Best: re-evaluate why you want to do this in the first place. If there are different actions that need to take place depending upon which form is submitted, why mash it all together? Sounds like a bad move to me.


Thanks for the reply Bear. This is for a page which enables the user to add/modify/delete DB entries, so i thought it would be user friendly to have it all display on one page. Only one of these displays at a given time, so its not as messy to the user
Using a hidden field like you said, would this work?


Thanks,
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61448
    
  67

Whether the forms are on the same page or not is completely immaterial as to how they are processed on the server.

Why are you trying to jam this all into a single servlet?

Stevie Shorey
Ranch Hand

Joined: Dec 10, 2012
Posts: 45

Bear Bibeault wrote:Whether the forms are on the same page or not is completely immaterial as to how they are processed on the server.

Why are you trying to jam this all into a single servlet?



That is very true. Yeah my reasoning (reducing amount of code/servlet files) was very faulty in trying to put everything in one place. Thanks!
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Stevie Shorey wrote:That is very true. Yeah my reasoning (reducing amount of code/servlet files) was very faulty in trying to put everything in one place. Thanks!

Just to add on, you cannot improves on lines of code by giving up cohesion and increasing the coupling among components. Think of the maintenance nightmares that would follow .


SCJP, SCWCD.
|Asking Good Questions|
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple Forms in Servlet/JSP