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
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 />---------------------
Joined: Apr 14, 2005
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
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