Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple Forms in Servlet/JSP

 
Stevie Shorey
Ranch Hand
Posts: 45
Android Chrome Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stevie Shorey
Ranch Hand
Posts: 45
Android Chrome Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
Android Chrome Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic