File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes what if doGet is called when it is not implemented? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "what if doGet is called when it is not implemented?" Watch "what if doGet is called when it is not implemented?" New topic

what if doGet is called when it is not implemented?

suma sindu
Ranch Hand

Joined: Apr 14, 2005
Posts: 35

In Javaranch's mock exam, the answer says that

an error is returned as the Servlet does not implement a doGet method.

But, somewhere else I THINK (not sure though) I read that the HttpServlet implements all doXXX() methods, so if u do not implement a method, say doGet() in your HttpServlet and say a user action results in doGet() being called, then the defalut doGet() is called(which doesn't do any thing), so it doesn't result in an error saying the method is not supported.

It's my understanding from somewhere else that if u override Service() method to call doPost() and doGet() gets called let's say by a user action, then even though u do not implement doGet(), doPost() is called and the result is from the doPost(). So, does it mean that if u override service() method and call some doXXX() method, then the actual request method is not served even when it was implemented?

Please clarify me whether a doXXX() method being called when it was not implemented will result in an error or whether the default is called which shouldn't result in any error.

(I'm not sure if my questions are clear. I hope they are ).

I'm planning to take my exam in the next week, but not sure if i'm ready yet, because when i do mock exams, i get some doubts which i never thout is a ??? for me

Anand Wadhwani
Ranch Hand

Joined: Mar 21, 2005
Posts: 151
I would suggest take some time, give at least 1000 questions before appearing in exam. Also take Head first Servlet and JSPs mock exam, because it's very popular that you get HFSJ mock exam score + 20% in real exam. S make sure you get more than 70% in HFSJ mock exam.

For your problem, I am not sure, because I am preparing myself and of course while preparing you dont learn tricky things which you do while doing mock questions.

Here is what I would say:

in case you do not override service() and only override doGet/doPost, in this case container executes service method from super class, that super implementation checks request header for knowing which http method is used. based on that service method internally would call doGet or doPost method, if appropriate method not found it will definitely throw error.

Now in second scenario, if you do override service() method so now it's your responsibility to handle heep request appropriately, see if request.getMethod() returns "get" or "post" and based on that call appropriate method or do nothing or take different actions or whatever, it's your custom handling actually.

Let us know what you find on this.

SCWCD 1.4<br />---------------------<br />Ability is what you're capable of. <br />Motivation determines what you do. <br />Attitude determines how well you do it.<br />---------------------
suma sindu
Ranch Hand

Joined: Apr 14, 2005
Posts: 35
I read somewhere that

"HttpServlet provides default implementation for all the doXXX methods. So even if you do not implement these methods there is no compliation error."

but this only talks about compilation time, but not about what happens when the request actually comes in for the method that was not implemented

chowdary Thammineedi
Ranch Hand

Joined: Aug 16, 2004
Posts: 126

There is NO default implementation of doGet and doPost methods in HttpServlet abstract class.

Let's take a look at the following

Tomcat's implementation of doGet() method in HttpServlet abstract class is

as you can see, it does nothing but send back an error to the client. You wonder why? Because, it is not supposed to be invoked in the first place.
(More on it below)

And the last time I checked, Tomcat does the EXACT same thing for
doPost() , doPut() , and doDelete().

The public service() method of the HttpServlet class just casts the ServletRequest and ServletResponse objects to HttpServletRequest and HttpServletResponse and calls the protected service() method.

Now, let's see what the protected service() method of HttpServlet does

It is very clear from the above code that the service method checks for the METHOD used to invoke this servlet and appropriately delegates calls.

In keeping with "JAVA's Run Time Type Identification" procedures, if your servlet class does not override the service method, the above service method handles all classes. It also dispathces as seen from above.

Dispatches to whom? Again RTTI plays a role. The calls will be dispatched to YOUR servlet. Because that's where the request is headed.

But then, what happens if HttpServlet's service() does not find an appropriate method in your class, the method invocation will come back to its own methods. Because you did not override them.

And what do the doGet(),doPost(),doPut() and doDelete() methods of HttpServlet do? Send an EROOR to the client.

If you want to see this class in all its glory you need to download the source for TOMCAT. Once you Unzip it you can find this class at


I guess you can substitute 5.5.9 for your version of Tomcat.
trivikram Kumar
Ranch Hand

Joined: Feb 21, 2005
Posts: 172
No there is no default implementations of these methods as I tried with Tomcat without a doGet() but it gave me an error.

Javainn<br /> <br />SCJP,SCWCD,SCBCD,<br />ICED (IBM-287,IBM-484,IBM-486)
IBM certified in DB2 V 8.1 Family Fundamentals (IBM 700)
chowdary Thammineedi
Ranch Hand

Joined: Aug 16, 2004
Posts: 126
There will be NO ERROR AT COMPILE time if you do not implement doget(), doPost(). The rule is you have to override ATLEAST one method, if you do not override the service() method.

But there definitely will be a Request time error, if your own servlet does not implement the method that the request asks for.( b/w doGet(), doPost(), doPut() and doDelete() )
trivikram Kumar
Ranch Hand

Joined: Feb 21, 2005
Posts: 172
Yes Chowdary...thats what I wanted to gives an error at request time and thats what i got...
I agree. Here's the link:
subject: what if doGet is called when it is not implemented?
It's not a secret anymore!