File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes overriding both doget() and dopost() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "overriding both doget() and dopost()" Watch "overriding both doget() and dopost()" New topic

overriding both doget() and dopost()

geet kaur
Ranch Hand

Joined: Sep 03, 2008
Posts: 79

If i override both doget() and dopost() in a servlet,then how is it decided that which method will be called???
Louis Bros
Ranch Hand

Joined: Jun 03, 2011
Posts: 54

It depends on the HTTP request.

If you use an HTML form to access your servlet and the method is set to post, then the doPost() method will be called.

<form method="post" action="">

If you set the form method to get or if you access the servlet directly via a url then the doGet() method will be called.

<form method="get" action="">

Also I believe if you don't set the form method then the default is a get request.

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63870

Actually, neither will be called as doget() and dopost() are not defined by the Servlet Spec. It's doGet() and doPost().

[Asking smart questions] [About Bear] [Books by Bear]
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1341

Hi Geet

If are asking your question from Front end (JSP / HTML) perspective then Loius has provided an explanation

But if are asking from server side perspective then answer is -->
once we send a GET/POST request to our server code then doService() method (generic method) decides whether to call doGet() OR doPost() depending upon request type
I agree. Here's the link:
subject: overriding both doget() and dopost()
It's not a secret anymore!