Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Making doPost call doGet

 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Core Servlets and JSPs, when a POST method is used, many of their examples have doPost() call doGet(). I'm just curious what is seen in the real world? I'm used to just using doPost(), even if there is no overriding doGet method. Is one way preferred over the other for some reason?
Thanks!
Jason
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your page can, and is accessed through doGet (i.e clicking on a link, typing the address in the URL), and you don't have a doGet method, you will get an error. If theere is a chance that your page can be accessed by both methods, it's good to handle both.
 
JeanLouis Marechaux
Ranch Hand
Posts: 906
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most of the time, the navigation through a web application is done using links. So the doGet() method has to be implemented.
And when you have forms, the POST method is often the one specified.
So the doPost() has to be implemented.
That's why the way it is done within Core servlets is a good one. Both methods are handled, but the script is only written once.
Another way is the IBM VAJ one.
it creates a performTask(request, response) methog, and doGet() doPost() call this method.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've implemented a similar solution going through the Cattle Drive Servlets and JDBC, and it works great (especially with subclasses). Mainly I was curious if you've written an HTML form the uses POST, would you really want to have doPost call doGet. But I guess after reading y'alls replies, it is just safer to get in the habit of implementing both just in case.
Thanks!
Jason
 
Robert Gagliardo
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to handle both doPost(...) and doGet(...) one simple way is to just overwrite the service(...) method, which calls doPost(...), doGet(...) and the other do*(...) methods.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic