Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Servlets and the fly likes doPost() and doGet() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "doPost() and doGet()" Watch "doPost() and doGet()" New topic

doPost() and doGet()

Chris Brat
Ranch Hand

Joined: May 22, 2003
Posts: 108
is it 'correct' to define both the doGet() and doPost() methods in a servlet (as in programatically correct) in order to keep the number of servlets loaded to a minimum or would it be better to only implement one per servlet but have more servlets (in an attempt to simplify design?)...
Is this personal preference?
Thanks again.

Neeraj Singhal
Ranch Hand

Joined: Aug 13, 2000
Posts: 49
Hi Chris
How does defining doGet() and doPost() affect the number of servlets loaded ??
it entirely depends on the requirement...whether u want to define doGet or doPost. BTW...u can define one and call it in another to achieve same functionality in both the methods.
[ September 30, 2003: Message edited by: Neeraj Singhal ]
Chris Brat
Ranch Hand

Joined: May 22, 2003
Posts: 108
I meant that, I might choose to define only one of the methods per servlet thereby needing more classes to do the same functionality as i would need if i had declared both methods.
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17271

"more classes to do the same functionality"
Well, that alone states that it would be a bad design of OOP principles. You want to keep functionality in one location and anyone that needs that functionality can call that one method. It is easier to maintain and enhance.
Sometimes you might want to consider pulling logic out into its own "helper class" that all Servlets can use and call.

Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Chris Brat
Ranch Hand

Joined: May 22, 2003
Posts: 108
thank u... that was what i was looking for.
I agree. Here's the link:
subject: doPost() and doGet()
jQuery in Action, 3rd edition