jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes which method will invoke? doGet() or doPost() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "which method will invoke? doGet() or doPost()" Watch "which method will invoke? doGet() or doPost()" New topic
Author

which method will invoke? doGet() or doPost()

Nagaraj Shivaklara
Ranch Hand

Joined: Dec 16, 2008
Posts: 77
Hi,

Can you please tell me which method will invoke in the below senario?

what happens if i submit a form without mentioning the method type(get or post) and in called servlet if i have only doPost() method; will doPost() method execute?

Thanks n Regards,
Nagaraj S K
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 510

If you don't specify a method type, by default it is a GET request, and if you have only doPost(), it doesn't get called, an HTTP status 405 is returned..

P:S: Why can't you try this yourself, instead of waiting for someone to give an answer for you?


Regards, Prasad
SCJP 5 (93%)
Janarthanan Rajendran
Greenhorn

Joined: Nov 07, 2011
Posts: 7

By default its GET , its always better to create a service method and call that inside doGet and doPost if your are planning to use both

Regards,
Jana
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61001
    
  65

Janarthanan Rajendran wrote:its always better to create a service method and call that inside doGet and doPost if your are planning to use both

No it is not. GET and POST should do different things. It's very short-sighted and a violation of the HTTP contract to just have them to the same thing.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nagaraj Shivaklara
Ranch Hand

Joined: Dec 16, 2008
Posts: 77
OK,

Then can you please tell me which method will invoke if i have only doPost method in servlet and did not mention any method type in the form submission?


Bear Bibeault wrote:
Janarthanan Rajendran wrote:its always better to create a service method and call that inside doGet and doPost if your are planning to use both

No it is not. GET and POST should do different things. It's very short-sighted and a violation of the HTTP contract to just have them to the same thing.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61001
    
  65

In your extending class? None. The base class will return a 405 error.
 
Don't get me started about those stupid light bulbs.
 
subject: which method will invoke? doGet() or doPost()